Default value to macro variable
NOTE: This is a great one we picked up from our friends over at the SAS community.
We have run across this literally hundreds of times while programming SAS macros. You need to have a default value for a variable and you don't want to write another macro to set it if it doesn't exist.
This simple fix allows you to check for a value and set a default even in open SAS code.
%global myParameter; /* ensure it exists */ %let myParameter = %sysfunc(coalescec(&myParameter,default-value));
Pure awesomeness..
Credit goes to Don Henderson and the SAS community for this one.
http://www.sascommunity.org/wiki/Tip_of_the_Day:October_12
SAS Do loops – Do While
Here is yet another Do Loop post.
The basic form of a do loop is as follows:
Data new_ds;
Set old_ds;
Do some_index_var= 1 to 50 by 1;
Some SAS statements go here;
End;
run;
However, specifically in this post we want to talk about DO WHILE loops. Do while loops are different because they evaluate the condition at the top of the loop. Example:
Data new_ds;
Set old_ds;
Do while var > 100;
Some SAS statements;
End;
Run;
This loop will not execute the loop if the condition is not met upon the first execution. This is what makes WHILE so valuable. Conditional processes that allows you to exit the loop before the first iteration.
Using SELECT groups for conditional processing
What are Select Groups?
Select Groups are another way SAS enables conditional processing of datasets.
When should we use Select Groups instead of if-then statements?
According to our friends at SAS, you should utilize Select Groups when your conditional criteria is mutually exclusive and numeric. When this is the case, Select Groups are more efficient than if-then processing.
Here are a few examples of conditional processing using Select Groups:
data NEW_DS;
set DS1;
select var1;
when (1) var2='YES';
when (2) var2='NO';
otherwise var2='UNKNOWN';
end;
run;
data NEW_DS;
set DS1;
select;
when (var1=1 and var2='JAN') var3='YES';
when (var1=2 and var2='FEB') var3='NO';
otherwise var3='UNKNOWN';
end;
run;
How to make SAS quit
When writing SAS macros and scripts, sometimes you need to tell SAS to exit or quit. We often use this when we are error handling. Here is a short example.
%macro test;
%let code_error=0;
< some SAS code >
%if &code_error=1%then %goto exit;
%mend test;
Let us know if we can help you with your SAS code or analytics project.
SAS Conditional programming – If Then
One of the great things about SAS is the use of Macros and conditional programming. The ability to split the code using If - Then processing allows one piece of code to accomplish many things. Here is the basic syntax for If - Then statements in a SAS macro.
%let error1=;
%if %length(&error1) ~=0 %then
%put WARNING: There is an error!;
%else
%put NOTE: There is no error.;
This is the basic example where you want to process one command given a condition. Lets say that you want to process a whole section of code depending on a single condition. See below:
%let error1=;
%if %length(&error1) ~=0 %then %do;
%put WARNING: There is an error!;
<and any other code you want to run here>
%end;
%else
%put NOTE: There is no error.;
<and any other code you want to run here>
%end;
Note that the %do command allows you to run a whole block of code. Be sure to include the %end statement at the end of the code block. Also, the %else portion of both examples are completely optional.
Check to see if a dataset exists
One of the checks we do many times in automated jobs is to constantly check to see if files and datasets are where we expect them to be. One of the easiest ways to do this is the macro language. We can use an IF statement with a system function. See below..
/*see if file exists*/
%if %sysfunc(exist(libname.dataset1)) %then %do ;
/*if it is here then*/
proc sql;
select some_data from some_table;
quit;%end;
%else %do;
/*if the file does not exist then*/%put WARNING: The file does not exist! ;
%goto exit;
%end;
To review, the above macro snippet checks to see if the dataset is there. If the dataset is there, then it runs a proc sql command. If it is not, it returns a WARNING and then exits.
Let us know if you a project that needs consultation or contract work.
www.AFHood.com