To Lecture Notes
The SAS Macro Language
What is a Macro?
- A macro is a program that writes source code
Why Use Macros?
- Here are three reasons for using SAS macros:
- To change the values of a small number of variables that change a SAS script in many locations.
- To define reusable code that can be run many times with variations in the same script or in
different scripts.
- Make the changes in your scripts data driven; that is modify your script based on values calculated
in one or more data steps.
- When running a standard SAS script, without macros or macro variables, the source code is passed to
the SAS compiler, and then immediately executed, producing the log and output files, and any files
written to disc as specified by file and put statements.
- When running a script with SAS macros and/or macro variables, the script is first sent to the
macro processor, which resolves any macro variables and uses any macros to write an equivalent SAS
script that does not use macro variables or macros. then this equivalent script is compiled and
executed.
Macro Variables
Then later in a data step, this macro name can be used like this:
first_user = "&user_name";
All macro variables have the & prefix when used, even though the &
prefix is not used when defining the macro variable.
When a macro variable is to be expanded in parentheses, double quotes must be used; macro variables are not expanded within single quotes.
Macro variables are expanded when they are in neither single or double quotes.
A period can be used to terminate a macro variable in the presence of ambiguity.
Practice Problems: Given these macro variable definitions:
%LET name = Illinois;
%LET name1 = Indiana;
%LET name2 = Wisconsin;
How does the macro processor translate these code fragments?
1. state = '&name';
2. state = "&name";
3. state = "&name1";
4. state = "&name.1";
5. state = "&name1.txt";
6. state = "&name.1.txt";
7. state = "&name..2.txt";
8. state = "&name..txt";
9. state = "&name2.txt";
10. state = "&name2..txt";
11. set &name1;
Look at the Tropical Example, Part 1
(Use a macro variable for simple text substitution).
Defining and Invoking a SAS Macro
The macro name in the %MEND statement is optional.
To invoke a macro, place it on a line by itself with no semicolon at the end:
%macro_name
Look at the Tropical Example, Part
2 (Define and invoke a macro).
Using Parameters with a Macro
To invoke a macro with parameters:
macro_name(param1=Value1, param2=Value2, param3=Value3)
In the macro definition, the parameters are called formal parameters.
In the macro invocation, the parameters are called actual parameters.
No quotes are used for macro actual parameters, even if they are character data.
No semicolon is used at the end of a macro invocation.
Look at the Tropical Example, Part
3 (Define and invoke a macro with parameters).
Using Control Statements in a Macro
These macro variables can be used anywhere in a SAS program:
&SYSDATE &SYSDAY
&SYSDATE returns the current date as a character variable: 1MAY13.
&SYSDAY returns the current day of the week: Monday.
Look at the Tropical Example, Part
4 (Use a macro with conditional logic).
CALL SYMPUT
- CALL SYMPUT assigns a value in a data step to a macro variable.
- Note: you cannot use a macro variable created with CALL SYMPUT in a datastep in that same data step because the variable
is not created until the datastop executes.
- Look at the Tropical Example, Part
5 (Use CALL SYMPUT to set macro variables).
Reference
- Delwiche and Slaughter, The Little SAS Book, a Primer, 4th Edition, SAS Publishing, 2011.