This section will give simple examples of two types of Macros: those that don't use macro variables, and those that do. The SAS Macro language and operation is documented fully in the Reference on macros shown at the end of this document.
Example 1: a simple macro
Say you are going to create ten different SAS data sets, each with the same variables but different data, and you want to print the same group of six variables after each data step completes. You can write the PROC PRINT and associated statements ten times, or you can write it once into a macro, then call the macro after each data step.
%macro print1;
proc print; var id height weight age iq birthyr;
run;
%mend print1;
data one; set master; if birthyr=1946;
%print1
data two; set master; if birthyr=1950;
%print1
data three; set master; if birthyr=1955;
%print1
(etc.)
Example 2: macros that use symbolic variables
Macros can be made even more flexible by the use of symbolic variables, which SAS calls "macro variables". In the example above, each DATA step has one value that changes, while the rest of the SAS language (including the macro %print1) stays the same. We can make that changing value (which is the value for BIRTHYR) into a macro variable, include the DATA step as well as the PROC PRINT in the macro itself, and then tell SAS which value of the macro variable to use each time it executes. The macro (symbolic) variable name is specified in parentheses after the name of the macro, then each time the macro is called the actual value to be used is specified.
%macro dataread(year);
data temp; set master; if birthyr=&year;
proc print; var id height weight age iq birthyr;
run;
%mend dataread;
%dataread(1946)
%dataread(1950)
%dataread(1955)
(etc.)