Chapter Contents

Previous

Next
%MACRO

%MACRO



Begins a macro definition

Type: Macro statement
Restriction: Allowed in macro definitions or open code
See also:
%MEND
SYSPBUFF


Syntax
Details
Examples
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

Syntax

%MACROmacro-name <(parameter-list)></ option(s)>;

macro-name
names the macro. A macro name must be a SAS name, which you supply; you cannot use a text expression to generate a macro name in a %MACRO statement. In addition, do not use macro reserved words as a macro name. (For a list of macro reserved words, see Appendix 1, "Reserved Words Used in the Macro Facility," in SAS Macro Language: Reference.)

parameter-list
names one or more local macro variables whose values you specify when you invoke the macro. Parameters are local to the macro that defines them. You must supply each parameter name; you cannot use a text expression to generate it. A parameter list can contain any number of macro parameters separated by commas. The macro variables in the parameter list are usually referenced in the macro.
parameter-list can be
<positional parameter-1><. . . ,positional paramter-n>
<keyword-parameter=<value> <. . . ,keyword-parameter-n=<value>>>

positional-parameter-1 <. . . ,positional-parameter-n>
specifies one or more positional parameters. You can specify positional parameters in any order, but in the macro invocation, the order in which you specify the values must match the order you list them in the %MACRO statement. If you define more than one positional parameter, use a comma to separate the parameters.

If at invocation you do not supply a value for a positional parameter, the macro facility assigns a null value to that parameter.

keyword-parameter=<value> <. . . ,keyword-parameter-n=<value>>
names one or more macro parameters followed by equal signs. Optionally, you can specify default values after the equal signs. If you omit a default value after an equal sign, the keyword parameter has a null value. Using default values allows you to write more flexible macro definitions and reduces the number of parameters that must be specified to invoke the macro. To override the default value, specify the macro variable name followed by an equal sign and the new value in the macro invocation.

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.  [cautionend]

option(s)
can be one or more of these optional arguments:

CMD
specifies that the macro can accept either a name-style invocation or a command-style invocation. Macros defined with the CMD option are sometimes called command-style macros.

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.

DES='text'
specifies a description for the macro entry in the macro catalog. Enclose the description in quotation marks. This description appears in the CATALOG window when you display the contents of the catalog containing the stored compiled macros. The DES= option is useful only when you use the stored compiled macro facility.

PARMBUFF
PBUFF
assigns the entire list of parameter values in a macro call, including the parentheses in a name-style invocation, as the value of the automatic macro variable SYSPBUFF. Using the PARMBUFF option, you can define a macro that accepts a varying number of parameter values.

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.

STMT
specifies that the macro can accept either a name-style invocation or a statement-style invocation. Macros defined with the STMT option are sometimes called statement-style macros.

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.

STORE
stores the compiled macro as an entry in a SAS catalog in a permanent SAS data library. Use the SAS system option SASMSTORE= to identify a permanent SAS data library. You can store a macro or call a stored compiled macro only when the SAS system option MSTORED is in effect. (For more information, see Chapter 9 in SAS Macro Language: Reference.)


Details

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.


Examples

Example 1: Using the %MACRO Statement with Positional Parameters

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 &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;

Example 2: Using the %MACRO Statement with Keyword Parameters

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;


Example 3: Using the %MACRO Statement with the PARMBUFF Option

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, PURPLE, RED, BLUE, and TEAL although the macro definition does not contain any individual parameters:

%printz(purple,red,blue,teal)

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;


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.