Generating SAS Code Using Macros

Defining Macros

Macros enable you to substitute text in a program and to do many other things. A SAS program can contain any number of macros, and you can invoke a macro any number of times in a single program.
To help you learn how to define your own macros, this section presents a few examples that you can model your own macros after. Each of these examples is fairly simple; by mixing and matching the various techniques, you can create advanced, flexible macros that are capable of performing complex tasks.
Each macro that you define has a distinct name. When choosing a name for your macro, it is recommended that you avoid a name that is a SAS language keyword or call routine name. The name that you choose is subject to the standard SAS naming conventions. (See the Base SAS language documentation for more information about SAS naming conventions.) A macro definition is placed between a %MACRO statement and a %MEND (macro end) statement, as in the following example:
%MACRO macro-name;
%MEND macro-name;
The macro-name specified in the %MEND statement must match the macro-name specified in the %MACRO statement.
Note: While specifying the macro-name in the %MEND statement is not required, it is recommended. It makes matching %MACRO and %MEND statements while debugging easier.
Here is an example of a simple macro definition:
%macro dsn;
   Newdata
%mend dsn;
This macro is named DSN. Newdata is the text of the macro. A string inside a macro is called constant text or model text because it is the model, or pattern, for the text that becomes part of your SAS program.
To call (or invoke) a macro, precede the name of the macro with a percent sign (%):
%macro-name
Although the call to the macro looks somewhat like a SAS statement, it does not have to end in a semicolon.
For example, here is how you might call the DSN macro:
title "Display of Data Set %dsn";
The macro processor executes the macro DSN, which substitutes the constant text in the macro into the TITLE statement:
title "Display of Data Set Newdata";
Note: The title is enclosed in double quotation marks. In quoted strings in open code, the macro processor resolves macro invocations within double quotation marks but not within single quotation marks.
The macro DSN is exactly the same as the following coding:
%let dsn=Newdata;

title "Display of Data Set &dsn";
The following code is the result:
title "Display of Data Set Newdata";
So, in this case, the macro approach does not have any advantages over the macro variable approach. However, DSN is an extremely simple macro. As you will see in later examples, macros can do much more than the macro DSN does.

Inserting Comments in Macros

All code benefits from thorough commenting, and macro code is no exception. There are two forms that you can use to add comments to your macro code.
The first form is the same as comments in SAS code, beginning with /* and ending with */. The second form begins with a %* and ends with a ;. The following program uses both types of comments:
%macro comment;
/* Here is the type of comment used in other SAS code. */
   %let myvar=abc;

%* Here is a macro-type comment.;
   %let myvar2=xyz;

%mend comment;
You can use whichever type comment that you prefer in your macro code, or use both types as in the previous example.
The asterisk-style comment ( * commentary ; ) used in SAS code is not recommended within a macro definition. While the asterisk-style will comment constant text appropriately, it will execute any macro statements contained within the comment. This form of comment is not recommended because unmatched quotation marks contained within the comment text are not ignored and can cause unpredictable results.

Macro Definition Containing Several SAS Statements

You can create macros that contain entire sections of a SAS program:
%macro plot;
   proc plot;
      plot income*age;
   run;
%mend plot;
Later in the program that you can invoke the macro:
data temp;
   set in.permdata;
   if age>=20;
run;

%plot

proc print;
run;
When these statements execute, the following program is produced:
data temp;
   set in.permdata;
   if age>=20;
run;

proc plot;
   plot income*age;
run;

proc print;
run;

Passing Information into a Macro Using Parameters

A macro variable defined in parentheses in a %MACRO statement is a macro parameter. Macro parameters enable you to pass information into a macro. Here is a simple example:
%macro plot(yvar= ,xvar= );
   proc plot;
      plot &yvar*&xvar;
   run;
%mend plot;
You invoke the macro by providing values for the parameters:
%plot(yvar=income,xvar=age)

%plot(yvar=income,xvar=yrs_educ)
When the macro executes, the macro processor matches the values specified in the macro call to the parameters in the macro definition. (This type of parameter is called a keyword parameter.)
Macro execution produces the following code:
proc plot;
   plot income*age;
run;

proc plot;
   plot income*yrs_educ;
run;
Using parameters has several advantages. First, you can write fewer %LET statements. Second, using parameters ensures that the variables never interfere with parts of your program outside the macro. Macro parameters are an example of local macro variables, which exist only during the execution of the macro in which they are defined.

Conditionally Generating SAS Code

By using the %IF-%THEN-%ELSE macro statements, you can conditionally generate SAS code with a macro. See the following example:
%macro whatstep(info=,mydata=);
   %if &info=print %then
      %do;
         proc print data=&mydata;
         run;
      %end;

   %else %if &info=report %then
      %do;
         options nodate nonumber ps=18 ls=70 fmtsearch=(sasuser);
      proc report data=&mydata nowd;
         column manager dept sales;
         where sector='se';
         format manager $mgrfmt. dept $deptfmt. sales dollar11.2;
         title 'Sales for the Southeast Sector';
      run;
   %end;
%mend whatstep;
In this example, the macro WHATSTEP uses keyword parameters, which are set to default null values. When you call a macro that uses keyword parameters, specify the parameter name followed by an equal sign and the value that you want to assign the parameter. Here, the macro WHATSTEP is called with INFO set to print and MYDATA set to grocery.
%whatstep(info=print,mydata=grocery)
This code produces the following statements:
proc print data=grocery;
run;
Because values in the macro processor are case sensitive, the previous program does not work if you specify PRINT instead of print. To make your macro more robust, use the %UPCASE macro function. For more information, see %UPCASE and %QUPCASE Functions.
For more information, see %MACRO Statement and %MEND Statement.