This post explains SAS macro programming with practical examples.
What are Macros
Introduction to SAS Macro Programming
I. Macro Variables
Macro Variables are of two types -
- Local - If the macro variable is defined inside a macro code, then scope is local. It would be available for use in that macro only and gets removed when the macro is finished.
- Global - If the macro variable is defined outside a macro code, then scope is global. It can be use any where in the SAS program and gets removed at the end of the session.
5 ways to create macro variables -
1. %LET
It can defined inside or outside a macro.
It can defined inside or outside a macro.
The syntax of the %LET statement -
%LET macro-variable-name = value;
%LET x = 5;
Example - To store system date
%let dt = &sysdate;
How to use Macro Variables
Macro variables are referenced by using ampersand (&) followed by macro variable name.
& <Macro variable Name>
To view in log window what macro variable would return, use %PUT statement :
%put &dt.;
%put NOTE : system data is &dt.;
Notice that unlike the PUT statement the text string is not enclosed in quotes. The quotes are not needed because, unlike in the DATA step, the macro facility does not need to distinguish between variable names and text strings.
2. Macro Parameters
2. Macro Parameters
Example : Suppose you are asked to write a macro that returns mean value of a variable. The analysis variable, input and output data sets are dynamic.
%macro test (input =, ivar=, output=);In the above code, test is a macro, input, ivar and output are local macro variables.
proc means data = &input noprint;
var &ivar;
output out = &output mean= ;
run;
%mend;
How to call a macro -
%test(input=sashelp.heart, ivar= height, output=test);
3. INTO clause in PROC SQL
Example : Calculate average height and store in a macro variable
proc means data = sashelp.heart noprint;
var height;
output out = test mean= avg_height;
run;
proc sql noprint;
select avg_height into :var1
from test;
quit;
%put &var1;
4. CALL SYMPUT routine
The syntax of CALL SYMPUT :
CALL SYMPUT(macro_varname,value);
data _null_;
set test;
call symput ('var2',avg_height);
run;
%put &var2;
5. ITERATIVE %DO
The syntax of iterative %DO -
%DO macro-variable = start %TO stop <%BY increment>;Example -
. . . text . . .
%END;
%macro calcl(start,stop);How to use conditional processing %IF %THEN ?
%do year = &start %to &stop;
data test;
set yr&year;
year = 2000 + &year;
run;
%end;
%mend calcl;
options mindelimiter=,;
options minoperator;
%MACRO test();
%DO i = 1 %to 9 ;
%if &i in (1,3,5,7,9) %then %do;%PUT i = &i - odd;
%END;
%ELSE %DO;%PUT i = &i - even;
%end;
%end;%MEND;
%test();
SAS Macro Functions
1. %EVAL Function
It is used to perform mathematical and logical operation with macro variables.
Example -
%let x = 10;It returns "10*20".
%let y = 20;
%let z = &x * &y;
%put &z;
%let z2 = %eval(&x*&y);
%put &z2;
It returns 200.
Note :
%let last2 = %sysevalf(4.5+3.2);
%put &last2;
2. %SYSFUNC Function
%let dt3 = %sysfunc(date(),yymmdd10.);
3. %STR Function
%let exmpl = %str(proc print; run;) ;
%put &exmpl;
4. %SCAN Function
It returns the nth word in a string.
%let var = var1 var2 var3;
%let varName =%scan(&var,1,%str( ));
%put &varName;
Concatenation of Macro Variables
Suppose you have 3 macro variables and the third variable is actually a concatenation of the first 2 variables' value.
%let x = var;
%let y = 1 ;
%let var1 = 25;
%let z = &&&x&y;
%put &x &y &z;
First && resolves to &, &x resolves to var, &y. resolves to 1.
There are some system options that can be used to debug SAS Macros:
1. MPRINT
MPRINT translates the macro language to regular SAS language. It displays all the SAS statements of the resolved macro code.
options mprint;2. MLOGIC
%macro test (input =,output=);
proc means data = &input noprint;
var height;
output out = &output mean= ;
run;
%mend;
%test(input=sashelp.heart,output=test);
options mlogic;
options mindelimiter=,;
options minoperator;
%MACRO test();
%DO i = 1 %to 9 ;
%if &i in (1,3,5,7,9) %then %do;
%PUT i = &i - odd;
%END;
%ELSE %DO;
%PUT i = &i - even;
%end;
%end;
%MEND;
%test();
3. SYMBOLGEN
When we use multiple ampersands (ex: &&&x&y) and SYMBOLGEN option prints the message in the LOG file about how the macro variable is resolved.
options symbolgen;
%let x = var;
%let y = 1 ;
%let var1 = 25;
%let z = &&&x&y;
Important Tips
1. Use double quotes to reference macro variables.
![]() |
SAS Macro |
2. The quotes are not needed in %PUT.
%put NOTE : system data is &dt.;
3. Use Proc PRINTTO for saving log in an external text file.
proc printto log="C:\Users\Deepanshu\Downloads\LOG2.txt" new;
run;
4. Clear LOG and OUTPUT Window
DM "Log" clear continue;
DM "Output" clear continue;