The code below executes a OS-command in a nice and easy fashion. It also returns the returncode (rc) for the execution of the command in the OS-environment.
options noxwait; data _null_; rc = system("copy c:\test\*.sas d:\test\"); put rc=; run;
The code below executes a OS-command in a nice and easy fashion. It also returns the returncode (rc) for the execution of the command in the OS-environment.
options noxwait; data _null_; rc = system("copy c:\test\*.sas d:\test\"); put rc=; run;
The code below makes a incremental/continuous variable called ID in a dataset.
data test; ID + 1; set test; run;
To save your SAS-program in SAS you can use the DM-command (Display Manager)
dm 'file c:\test\test.sas'; or dm 'file "&MacroVariable"';
I think the best way to start a SAS-program is to do a comment as described by the template below.
/******************************************************************************** Author : Creation date : ddmmmyyy Description : Example : ********************************************************************************* Input ----- &InputMacro : Macrovariable with inputs. InputDS : Tells what dataset to do the processing on. ********************************************************************************* Output ------ &OutputMacro : Macrovariable with the output result. OutputDS : Dataset with the output result. ********************************************************************************/
Comments should also be done above each datastep or procedure, And changes to the programs should be contained in a versioning system eg like Subversion (SVN).If you do not have a versioning system, then I think the comments should be something like the comments below.
/* AUTHOR <ISODATE>: Changed one thing to another. */
The display manager (also known as DM) in base-SAS can be used for data exploration. For this demonstration the dataset sashelp.class will be used. Now sashelp.class is easy to get an overview of because it only has five variables. But if you have a lot of variables then some kind of data exploration/data manipulation might be handy.
Let’s say that you would like to take a look at the varable Age. In the DM you will write keep age.
This will result in a displaying of the data only showing the variable age. Now you would like to have all the other variables shown but keep age as the first variable being displayed. This can be done writing the command unide _all_ in the DM.
Now all the variable will be shown with age as the first variable.
It is also possible to keep multiple variables. In the DM you can eg. write keep ‘height weight’. Remember that when keeping multiple variables you will have to write the variables in ‘ ‘.
Now only these variables will be shown.
You can again unhide the rest of the variables writing unhide _all_ this will keep the height and weight variables as the first variables being shown.
The code below will make a file class.sql containing the SQL-code for creating the dataset sashelp.class
ods tagsets.sql file="class.sql"; proc print data=sashelp.class ; run; ods _all_ close;
The file will look something like this
Create table CLASS
(Name varchar(7), Sex varchar(1), Age float, Height float, Weight float);
Insert into CLASS(Name, Sex, Age, Height, Weight)
Values (‘Alfred’, ‘M’, 14, 69.0, 112.5);
Insert into CLASS(Name, Sex, Age, Height, Weight)
Values (‘Alice’, ‘F’, 13, 56.5, 84.0…
The code below shows you how to make an empty dataset in SAS.
If you omit the if-sentence and below, then you will get an empty row in the dataset.
data StatusTable; length Dataset $100 Message $150 Status $10; call missing(Dataset, Message, Status); if _N_ = 0 then output; stop; run;
This can also be done a bit easier in SQL.
proc sql noprint; create table Dataset ( Dataset char 100, Message char 150, Status char 10 ); quit;
The code below compares two datasets. It merges them together and makes three datasets. One dataset contains identical observations from the two datasets. The second dataset contains observations only found in one dataset. And the third dataset contains observations only found in the other dataset.
data InBoth InOne InTwo; merge One (in=a) Two (in=b); by <variables>; if a and b then output InBoth; if a and not b then output InOne; if b and not a then output InTwo; run;
The link below has a lot of good SAS macros.
The code below shows how to return a value from a SAS macro.
/* Returns 0 if a specific variable is not found in a dataset and 1 if it is found. */ %macro VarExist(_Dataset=, _Variable=); %local dsid rc ; %let dsid = %sysfunc(open(&_Dataset)); %if (&dsid) %then %do; %if %sysfunc(varnum(&dsid,&_Variable)) %then 1; %else 0 ; %let rc = %sysfunc(close(&dsid)); %end; %else 0; %mend VarExist; /* Go to do something if the dataset sashelp.class contains the variable age - and it does. */ %if %VarExist(_Dataset=sashelp.class, _Variable=age) %then %do; /* Something */ %end;
It is also possible to return a value from a macro using the code below. This only works for simple macros.
%macro add(no1=, no2=); %let result = %eval(&no1 + &no2); &Result %mend; %let sum = %add(no1=2, no2=2);