
Introduction to SAS Macro Programming
last updated: 19FEB2002

- What is a Macro?
- Are there any rules when using macros?
- Basic Example
- What is a Macro variable?
- Defining Macro variables
- Special Characters in macro variables
- How do I check the value of a macro variable?
- Modular Construction of Macros
- Changing values in a Macro
- Parametrization of Macros
- Conditionals
- Notes…
- References, resources and examples
-
A macro is a programming code which consists of system variables,
statements and functions which are directly processed by the SAS
via the macro processor. A macro can save processing time and yield
resource more efficient code, specially in situations when the same code
is needed or you want a more dynamic processing control. If you are
familiar with other programming languages, a macro is much like a
subroutine.
The macro facility is invoked when SAS encounters one of two
characters, the ampersand [&] or the
percent [%], followed by the macro variable name [nonblank character].
Once a macro is defined it can be called from anywhere in your SAS job.
You can defined a macro with variables whose values are used ONLY within
that macro, or, you can have variables used globally in your SAS
program but which are defined and interpreted via the macro processor.
In the first case, we are referring to a macro, which is a set
of text which written in “macro language”. The second case refers to a
macro variable is a variable defined via the macro
processor in such language whether is used inside or outside any
given macro.
Are there any rules when using macros?
Yes, there are some rules we need to keep in mind when defining macros.
- A macro has to defined before it can be called.
- A macro must be started with..
%MACRO macroname - A macro MUST be ended with…
%MEND macronameIf you fail to end a macro, the macro facility will continue reading all
text following as if it is part of the macro. - A macro can contain:
- DATA and PROC steps codes, and
- macro programming statements and functions.
- There are two types of macro references:
- Macro variables references, which are preceeded by the ampersand [&].
- Macro call and definitions, which are preceeded by the percent [%].
- A macro can be named with any valid SAS name, except for the set of
reserved words. - Macros are ONLY resolved if referred to inside of double quotes.
- A comment statement in a macro is prompted by: %*
- SAS ONLINE
Samples
%macro 2plot; proc plot; plot xvar*yvar; run; %mend 2plot;
- When a macro variable is found in the code, a SAS variable is
- system defined
-
Examples:
- &sysdate -date when the SAS session started
- &sysday -name of the day of the week when the SAS session started.
- &sysinfo -diagnostic information from some procs
- &systime -time of day the SAS session was started
- &sysver -SAS release number
- &sysvlong -SAS release number including the TS level
- user defined. The format for an user defined macro variable is…
¯ovariablenameIn some cases where the macro variable reference is immediately followed
by a letter, digit, underscore, or period, a period has to follow the
macro variable name.
¯ovariablename.
created. The values for current macro variables are stored in internal SYMBOL
TABLES. Other naming conventions are identical to those used in SAS.
There are two types of SAS macro variables:
If the SAS macro facility fails to find the value for a macro variable
you will get an error: “Warning: Apparent symbolic reference is not
resolved”.
- %LET
-
The %LET statement is used to define a macro variable and it can be used
anywhere within a SAS program. The form of this statement is:%LET macrovariablename= value ;
where value is the value to be set to the macro variable. If ommited, it
is set to null. Trailing and leading blanks are ignored. All characters
are used, including any quotes in the value.
Special Characters in macro variables
-
To include special characters as part of the macro variable value, use the
%STR or the %NRSTRfunctions. %STR hides the meaning of special
characters and blanks but not the meaning of & and %.The %NRSTR function allows you to include
the ampersand or percent sign as part of the macro variable value. - Examples:
%LET p1= proc print;%LET p2=%str(&p1;run;);
%LET p3=%nrstr(&p2;run;);
P2 will yield the code proc print;run;, and
p3 will yield &p2;run;. The latter needs
having set a value for the macrovariable p2 for it to run.
How do I check the value of a macro variable?
-
Use the %PUT statement to write to the SAS LOG window the value of the
variable of interest. Remember to reference the macro variable with an
ampersand. If you instead of a reference use _ALL_, you will get a list of
all macro variables currently set.
%PUT &p1;
%PUT _ALL_;
%PUT NOTE: This is my note to be written in the
SASLOG in blue;
Modular Construction of Macros
-
Sometimes you might need to create a macro from which other macros are
called. This is called “nesting” macros. So we have the macro named
%2plot [See above]. We now want
to take this macro and run it but inside of a new macro.
Lets first define
a second macro which we will call %readit. This macro will
contain a DATA step which reads in a data file, filtering those cases
where gender is “FEMALE”.
%macro readit; data temp; set purple.file1; if gender="F"; run; %mend readit;
Now, I can define the macro I had in mind which calls both %2plot
and %readit.
%macro analisis; %readit; %2plot; %mend analisis;
-
If you want you can also change the values used in a macro by setting
macro variables BEFORE you call in the macro. Lets use again the example
of the macro %2plot, but lets make
xvar and yvar macrovariables instead…
%macro 2plot; proc plot; plot &xvar*&yvar; run; %mend 2plot;
I now can submit the following code…
%let yvar=salary;%let xvar=gender;%2plot;
Or…
%let yvar=score;%let xvar=gender;%2plot;
Note that this will effectively yield similar results than with
defining parameters in the macro call.
-
What if I could write the same macro %2plot but making it
flexible for any 2 variables from any file I open without having
to make changes to macro variables? …
Well, we can!
This process implies providing some parameter values to the macro. The
number of parameter values depend on how you define the macro.
Notice that the arguments in this macro are positional. That is:
the first one is substituted inside the
macro statements whereever &xvar occurs; the second one is substituted
whereever &yvar occurs. Make sure though, that you remember the order of
the arguments, and that you supply values for all parameters.
In the following example, we define the macro such that two parameters are
expected upon calling it. Additionally, we set default values for the
most typical possibility [You can ommit setting default values by not
writing the equal sign and the value].
%macro 2plot(yvar=score,xvar=age); proc plot; plot xvar*yvar; run; %mend 2plot;
Then, when I call the macro %2plot(), I will enter…
%2plot(gender, age);
Or…
%2plot(score, race);
Or…
%2plot(income, age);
The possibilities are endless! Since I defined this macro having default
values, entering…
%2plot();
will give me the plot of score vs. age.
-
We could also write a macro which executes different steps depending a
conditional statement.
%macro 2plot(yvar=score,xvar=age); %if &yvar<1 %or &xvar<1 %then proc plot; plot xvar*yvar; run; %mend 2plot;
- Macros and other SAS commands can be stored in an external file which
can be included in your current program and stored in a file called
macro.sas.
/* include previously defined macros */%include ‘macro.sas’;
References, resources and examples
- SAS Guide to Macro Processing, Version 6, Second Edition
- Macros
and Applications from SAS Institute - A complex Example
- SAS
macro facility - SAS
Macros— A. Schick - SAS Graphic
Programs and Macros— M. Friendly - Introduction
to the SAS macros language