|Restriction:||Allowed in macro definitions or open code|
|Example 1: Using the %MACRO Statement with Positional Parameters|
|Example 2: Using the %MACRO Statement with Keyword Parameters|
|Example 3: Using the %MACRO Statement with the PARMBUFF Option|
|%MACROmacro-name <(parameter-list)></ option(s)>;|
|parameter-list can be
Note: You can
define an unlimited number of parameters. If both positional and keyword parameters
appear in a macro definition, positional parameters must come first.
Use the CMD option only for macros you plan to execute from the command line of a SAS window. The SAS system option CMDMAC must be in effect to use command-style invocations. If CMDMAC is in effect and you have defined a command-style macro in your program, the macro processor scans the first word of every SAS command to see whether it is a command-style macro invocation. When the SAS system option NOCMDMAC option is in effect, the macro processor treats only the words following the % symbols as potential macro invocations. If the CMDMAC option is not in effect, you still can use a name-style invocation for a macro defined with the CMD option.
If the macro definition includes both a set of parameters and the PARMBUFF option, the macro invocation causes the parameters to receive values and and also causes the entire invocation list of values to be assigned to SYSPBUFF.
To invoke a macro defined with the PARMBUFF option in a windowing environment or interactive line mode session without supplying a value list, enter an empty set of parentheses or more program statements after the invocation to indicate the absence of a value list, even if the macro definition contains no parameters.
The IMPLMAC system option must be in effect to use statement-style macro invocations. If IMPLMAC is in effect and you have defined a statement-style macro in your program, the macro processor scans the first word of every SAS statement to see whether it is a statement-style macro invocation; when the NOIMPLMAC option is in effect, the macro processor treats only the words following the % symbols as potential macro invocations. If the IMPLMAC option is not in effect, you still can use a name-style invocation for a macro defined with the STMT option.
The %MACRO statement begins the definition of a macro, assigns the macro a name, and optionally can include a list of macro parameters, a list of options, or both.
A macro definition must precede the invocation of that macro in your code. The %MACRO statement can appear anywhere in a SAS program, except within data lines. A macro definition cannot contain a CARDS statement, a DATALINES statement, a PARMCARDS statement, or data lines. Use an INFILE statement instead.
By default, a defined macro is an entry in a SAS catalog in the WORK library. You also can store a macro in a permanent SAS catalog for future use. However, in Version 6 and earlier, SAS does not support copying, renaming, or transporting macros.
You can nest macro definitions, but doing so is rarely necessary and is often inefficient. If you nest a macro definition, then it is compiled every time you invoke the macro that includes it. Instead, nesting a macro invocation inside another macro definition is sufficient in most cases.
In this example, the macro PRNT generates a PROC PRINT step. The parameter in the first position is VAR, which represents the SAS variables that appear in the VAR statement. The parameter in the second position is SUM, which represents the SAS variables that appear in the SUM statement.
%macro prnt(var,sum); proc print data=srhigh; var &var; sum ∑ run; %mend prnt;
In the macro invocation, all text up to the comma is the value of parameter VAR; text following the comma is the value of parameter SUM.
%prnt(school district enrollmt, enrollmt)
During execution, macro PRNT generates these statements:
PROC PRINT DATA=SRHIGH; VAR SCHOOL DISTRICT ENROLLMT; SUM ENROLLMT; RUN;
In the macro FINANCE, the %MACRO statement defines two keyword parameters, YVAR and XVAR, and uses the PLOT procedure to plot their values. Because the keyword parameters are usually EXPENSES and DIVISION, default values for YVAR and XVAR are supplied in the %MACRO statement.
%macro finance(yvar=expenses,xvar=division); proc plot data=yearend; plot &yvar*&xvar; run; %mend finance;
The macro processor generates this SAS code:
PROC PLOT DATA=YEAREND; PLOT EXPENSES*DIVISION; RUN;
Because the value of YVAR did not change, it retains its default value. Macro execution produces this code:
PROC PLOT DATA=YEAREND; PLOT EXPENSES*YEAR; RUN;
The macro PRINTZ uses the PARMBUFF option to allow you to input a different number of arguments each time you invoke it:
%macro printz/parmbuff; %let num=1; %let dsname=%scan(&syspbuff,&num); %do %while(&dsname ne); proc print data=&dsname; run; %let num=%eval(&num+1); %let dsname=%scan(&syspbuff,&num); %end; %mend printz;
This invocation of PRINTZ contains four parameter values,
although the macro definition does not contain any individual parameters:
As a result, SAS receives these statements:
PROC PRINT DATA=PURPLE; RUN; PROC PRINT DATA=RED; RUN; PROC PRINT DATA=BLUE; RUN; PROC PRINT DATA=TEAL; RUN;
Top of Page
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.