Extending and customizing SAS for reducing the amount of text that the programmer needs to do common tasks. SAS macro enables the programmer to assign a name to a character string or group of SAS programming statements.
The names of macro variables are prefixed with an ampersand (&) while the names of macros are prefixed with a percent sign (%). A macro variable is like a standard data variable except that, having only a single value, it does not belong to a data set, and its value is always a character. This value could be a variable name, a numeral, or any text that you want to be substituted into your program.
If you are writing the same or similar SAS statements over and over, you should consider using a macro. A macro lets you package a piece of bug-free code and use it repeatedly within a single SAS program or in many SAS programs. You can think of a macro as a kind of sandwich. The %MACRO and %MEND statements are like two slices of bread. Between those slices, you can put any statements you want.
SAS macro is a string-based language.
2. Defining a macro variable
%LET is used to define a macro variable.
%let macro_var = risk_score;
The macro variable is referenced in the code using the ampersand sign (&).
proc means data=file1; var ¯o_var.; run;
3. Defining macro programming
%MACRO is used to define a macro program.
%macro macro_print; proc print data=file1 (obs=10); run; %mend;
Macro program is referenced in the code using percentage sign (%)
4. Defining macro programming with macro variables
Macro variables are defined after the macro name. The variables created in the macro are local variables.
%macro macro_means (var); proc means data=file1; var &var; run; %mend;
The macro variables are passed in the macro when the macro program is referenced.
5. Defining macro programming with macro variables having a default value
Macro variables are defined after the macro name. The default value is defined so that in case no value is passed then the default value is picked.
%macro macro_means_def (var=risk_score); proc means data=file1; var &var; run; %mend;
When no value is passed the default value ‘risk_score’ is picked by the macro variable.
6. AutomaticSAS macro variables
SYSDATE is the date when the SAS is invoked. SYSDATE9 is the date when SAS is invoked in DDMMMYYYY format. SYSDAY is the day of the week when SAS is invoked. SYSTIME is the time when SAS is invoked.
%put &sysdate; %put &sysdate9; %put &sysday; %put &systime;
The below SAS macros change based on the submitted SAS statements. SYSLAST is the name of the most recent SAS dataset created. SYSERR contains the error code if some data step or proc step has not executed properly.
%put &syslast; %put &syserr;
To verify the value of a macro variable or to write your own message in the SAS LOG ‘put’ statement can be used.
7. STR function
It is used to mask tokens during compilation so that the macro processor does not interpret them as macro-level syntax. Special tokens and mnemonics include ; + – * / IN < LT LE > GT GE = EQ NE ~ NOT ^ AND | OR
Option 1 – the full syntax is masked
%let var_means1 = %str(proc means data=file1; var risk_score; run;); &var_means1;
Option 2 – only the semi-colon is masked
%let var_means2 = proc means data=file1 %str(;) var risk_score %str(;) run %str(;); &var_means2;
8. Adding text before and after macro
The programmer can place text immediately before a macro variable as long as the macro variable is placed immediately after the ampersand (&)
%macro text_before (var); proc means data=file1; var risk_score&var.; run; %mend; %text_before(); %text_before(_2); %text_before(_3);
The programmer can place text immediately after a macro variable as long as the macro variable is placed immediately after the ampersand (&) before the period (.)
%macro text_after (var); proc means data=file1; var &var. &var._2 &var._3; run; %mend; %text_after(risk_score);
9. Quotes are included in the macro
The quotes are included as part of the macro variable. The output of the below code is – 2020 as “COVID-19” year
%let var1 = "COVID-19"; %let var2 = 2020 as &var1 year ; %put &var2;
The output of the below code is – 2021 as ZOMBIE year
%let var1 = ZOMBIE; %let var2 = 2021 as &var1 year ; %put &var2;
10. Double vs single quote
The macro processor resolves macro variable references within double quotes and not within single quotes. The output of the below code is – “HAPPY NEW YEAR”
%let var3 = HAPPY NEW YEAR; %put "&var3";
The output of the below code is – ‘&var3’
11. Defining the macro variable in the data step
CALL SYMPUT is used to assign a data set variable as a value to a macro variable. It can also be used to create a series of macro variables in one data step. This function automatically converts a numerical value to a character value when used to assign a value to a macro variable.
data _null_; call symput ('yyyy',2021); run; %put &yyyy;
12. Defining the macro variable in proc step
The programmer can create or update macro variables during the execution step for proc sql. INTO clause is used to define the macro variable.
proc sql noprint; select avg(ext_quality_score) into: avg_score from file2; quit; %put &avg_score;
13. Creating multiple macro variables in proc sql
Multiple macro variables can be created in the proc sql step using INTO clause. Each macro variable should be preceded by colon (:)
proc sql noprint; select min(ext_quality_score), avg(ext_quality_score), max(ext_quality_score) into:min_score, :avg_score, :max_score from file2; quit;
The output of the below code is min_score = 0.00, avg_score = 0.62 and max_score = 1.00.
%put &min_score; %put &avg_score; %put &max_score;
14. Resolving multiple
Macro processor scans and resolves tokens from left to right from the point where multiple ampersands are added until no more triggers can be resolved. This is also known as indirect referencing.
%let teacher3 = MR D K BOSE; %let n = 3;
The output of the below code is – MR D K BOSE
The output of the below code is – MR D K BOSE
15. Options related to macro
These options are used to debug the SAS macros. SYMBOLGEN prints the value of the macro variable in the SAS LOG. MPRINT sends the text to the compiler when a macro is executed and is printed in the SAS LOG. MLOGIC prints the message that indicates macro actions that were taken during macro execution.
options symbolgen mprint mlogic;
16. Positional parameters
The programmer needs to define the values of the macro variables in the same order that they are defined.
%macro macro_means_pos (d, v); proc means data=&d; var &v; run; %mend; %macro_means_pos(file2, ext_quality_score);
17. Parameter keywords
The programmer must specify the macro variable followed by equal to sign. The order of the values of the macro variables can be random.
%macro macro_means_key (d=, v=); proc means data=&d; var &v; run; %mend;
The two statements below give the same output.
%macro_means_key(d=file2, v=ext_quality_score); %macro_means_key(v=ext_quality_score, d=file2);
The programmer can also use mixed parameters.
18. Eval and Sysevalf
To perform mathematical actions on the macro variables having integer values. EVAL function is used. The value of the macro variable ‘c’ is 11.
%let a = 5; %let b = 6; %let c = %eval(&a+&b); %put &c;
To perform mathematical actions on the SAS macros variables having float values. SYSEVALF function is used. The value of the macro variable ‘c’ is 12.
%let a = 5.5; %let b = 6.5; %let c = %sysevalf(&a+&b); %put &c;
The media shown in this article are not owned by Analytics Vidhya and is used at the Author’s discretion.You can also read this article on our Mobile APP