Using Macro Parameters

You have seen the basic form for a macro definition. Your macros often contain macro variables. To make your macros more dynamic, you could use the %LET statement to update the values of the macro variables that are used within the macros. However, parameter lists in your macro definitions enable you to update the macro variables within your macro programs more conveniently. A parameter list is an optional part of the %MACRO statement that names one or more macro variables whose values you specify when you call the macro.

Example

Suppose the compiled macro Printdsn contains references to the macro variables dsn (which records a data set name) and vars (which records a list of data set variables), as follows:
%macro printdsn;
   proc print data=&dsn;
      var &vars;
   title "Listing of %upcase(&dsn) data set";
   run;
%mend;
You could modify the behavior of Printdsn by changing the value of macro variable dsn or vars with a %LET statement before you call the macro. For example, you could substitute sasuser.courses for dsn and course_code course_title days for vars at macro execution, as follows:
%let dsn=sasuser.courses;
%let vars=course_code course_title days;
%printdsn
If the MPRINT system option is turned on when this code is submitted, the following messages are written to the SAS log. Notice that the values that you provided in the %LET statements have been substituted into the macro when it appears in the SAS log.
Table 11.5 SAS Log
7     options mprint;
8     %let dsn=sasuser.courses;
9     %let vars=course_code course_title days;
10    %printdsn
NOTE: Writing HTML Body file: sashtm.htm
MPRINT(PRINTDSN):  proc print data=sasuser.courses;
MPRINT(PRINTDSN):  var course_code course_title days;
MPRINT(PRINTDSN):  title "Listing of SASUSER.COURSES data set";
MPRINT(PRINTDSN):  run;
NOTE: There were 6 observations read from the data set
      SASUSER.COURSES.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           6.59 seconds
      cpu time            0.28 seconds
Then you could submit new %LET statements in order to change the value of dsn to sasuser.schedule and to change the value of vars to course_code location begin_date when the macro executes, as follows:
%let dsn=sasuser.schedule;
%let vars=course_code location begin_date;
%printdsn
The messages that are written to the SAS log when this code is submitted show that the new values have been substituted for the macro variable references in the macro.
Table 11.6 SAS Log
11   %let dsn=sasuser.schedule;
12   %let vars=course_code location begin_date;
13   %printdsn
MPRINT(PRINTDSN):   proc print data=sasuser.schedule;
MPRINT(PRINTDSN):   var course_code location begin_date;
MPRINT(PRINTDSN):   title "Listing of SASUSER.SCHEDULE data set";
MPRINT(PRINTDSN):   run;
NOTE: Writing HTML Body file: sashtm1.htm
NOTE: There were 18 observations read from the data set
      SASUSER.SCHEDULE.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.76 seconds
      cpu time            0.08 seconds
You can make these macro variables easier to update by using parameters in the macro definition to create the macro variables. Then you can pass values to the macro variables each time you call the macro rather than using separate %LET statements. The next few sections show how to use various types of parameters to create macro variables.

Macros That Include Positional Parameters

When you include positional parameters in a macro definition, a macro variable is automatically created for each parameter when you call the macro. To define macros that include positional parameters, you list the names of macro variables in the %MACRO statement of the macro definition. Positional parameters are so named because the order in which you specify them in a macro definition determines the order in which they are assigned values from the macro call. That is, when you call a macro that includes positional parameters, you specify the values of the macro variables that are defined in the parameters in the same order in which they are defined.
General form, macro definition that includes positional parameters:
%MACRO macro-name(parameter-1<,...,parameter-n>);
text
%MEND <macro-name>;
Here is an explanation of the syntax:
parameter-1<,...,parameter-n>
specifies one or more positional parameters, separated by commas. You must supply each parameter with a name: you cannot use a text expression to generate it.
To call a macro that includes positional parameters, precede the name of the macro with a percent sign, and enclose the parameter values in parentheses. List the values in the same order in which the parameters are listed in the macro definition, and separate them with commas, as follows:
%macro-name(value-1<,...,value-n>)
The values listed in a macro call
  • can be null values, text, macro variable references, or macro calls
  • are assigned to the parameter variables using a one-to-one correspondence.

Example

You can use positional parameters to create the macro variables dsn and vars in the Printdsn macro definition, as follows:
%macro printdsn(dsn,vars);
   proc print data=&dsn;
      var &vars;
   title "Listing of %upcase(&dsn) data set";
   run;
%mend;
In this case, when you call the Printdsn macro you assign values to the macro variables that are created in the parameters. In the following example, the value sasuser.courses is assigned to the macro variable dsn, and the value course_code course_title days is assigned to the macro variable vars. Notice that the value for dsn is listed first and the value for vars is listed second, since this is the order in which they are listed in the macro definition.
%printdsn(sasuser.courses,course_code course_title days)
Note: To substitute a null value for one or more positional parameters, use commas as placeholders for the omitted values, as follows:
%printdsn(,course_code course_title days)

Macros That Include Keyword Parameters

You can also include keyword parameters in a macro definition. Like positional parameters, keyword parameters create macro variables. However, when you use keyword parameters to create macro variables, you specify the name, followed by the equal sign, and the value of each macro variable in the macro definition.
Keyword parameters can be listed in any order. Whatever value you assign to each parameter (or variable) in the %MACRO statement becomes its default value. Null values are allowed.
General form, macro definition that includes keyword parameters:
%MACRO macro-name(keyword-1=<value-1><,...,keyword-n=<value-n>>);
text
%MEND <macro-name>;
Here is an explanation of the syntax:
keyword-1=<value-1><,...,keyword-n=<value-n>>
names one or more macro parameters followed by equal signs. You can specify default values after the equal signs. If you omit a default value, the keyword parameter has a null value.
When you call a macro whose definition includes keyword parameters, you specify the keyword, followed by the equal sign, and the value for each parameter. The order is not important. If you omit a keyword parameter from the macro call, the keyword variable retains its default value, as follows:
%macro-name(keyword-1=value-1<,...,keyword-n=value-n>)

Example

You can use keyword parameters to create the macro variables dsn and vars in the Printdsn macro. This example assigns a default value of sasuser.courses to the macro variable dsn and assigns a default value of course_code course_title days to the macro variable vars:
%macro printdsn(dsn=sasuser.courses,
               vars=
course_code course_title days);
   proc print data=&dsn;
      var &vars;
   title "Listing of %upcase(&dsn) data set";
   run;
%mend;
To invoke the Printdsn macro with a value of sasuser.schedule for dsn and a value of teacher course_title begin_date for vars, issue the following call:
%printdsn(dsn=sasuser.schedule, vars=teacher course_code begin_date)
To call the Printdsn macro with default values for the parameters (sasuser.courses as the value for dsn and course_codecourse_title days as the value for vars), you could issue the following call:
%printdsn()
Note: To call the macro Printdsn with default values for the parameters, you could also issue a macro call that specified these values explicitly, as follows:
%printdsn(dsn=sasuser.courses,vars=course_code course_title days)

Macros That Include Mixed Parameter Lists

You can also include a parameter list that contains both positional and keyword parameters in your macro definitions. All positional parameter variables in the %MACRO statement must be listed before any keyword parameter variable is listed.
General form, macro definition that includes mixed parameters:
%MACRO macro-name(parameter-1<,...,parameter-n>,
keyword-1=<value-1><,...,keyword-n=<value-n>>);
text
%MEND;
Here is an explanation of the syntax:
parameter-1<,...,parameter-n>
is listed before keyword-1=<value-1><,...,keyword-n=<value-n>>.
Similarly, when you call a macro that includes a mixed parameter list, you must list the positional values before any keyword values, as follows:
%macro-name(value-1<,...,value-n>,
            keyword-1=value-1<,...,keyword-n=value-n>)

Example

You can use a combination of positional and keyword parameters to create the macro variables in the Printdsn macro definition. This code uses a positional parameter to create the macro variable dsn, and a keyword parameter to create the macro variable vars:
%macro printdsn(dsn, vars=course_title course_code days);
   proc print data=&dsn;
      var &vars;
   title "Listing of %upcase(&dsn) data set";
   run;
%mend;
The following call to the Printdsn macro assigns the value sasuser.schedule to the macro variable dsn and assigns the value teacher location begin_date to the macro variable vars. Notice that the value for dsn is listed first, since dsn is the positional parameter.
%printdsn(sasuser.schedule, vars=teacher location begin_date)
Now, suppose you want to execute the Printdsn macro, assigning the default value course_title course_code days to the macro variable vars and assigning the value sasuser.courses to the macro variable dsn. You could issue the following call:
%printdsn(sasuser.courses)
Because this call omits the keyword parameter (vars), the default value for that parameter is used.

Macros That Include the PARMBUFF Option

You can use the PARMBUFF option in a macro definition to create a macro that can accept a varying number of parameters at each invocation. The PARMBUFF option 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.
General form, macro definition with the PARMBUFF option:
%MACRO macro-name /PARMBUFF;
text
%MEND;
Here is an explanation of the syntax:
text
contains a reference to the automatic macro variable SYSPBUFF.

Example

The following macro definition creates a macro named Printz. Printz uses a varying number of parameters and the automatic macro variable SYSPBUFF to display the parameters that are specified in the macro call. The macro also uses a loop to print the data sets that are named as parameters.
%macro printz/parmbuff;
   %put Syspbuff contains: &syspbuff;
   %local num;
   %do num=1 %to %sysfunc(countw(&syspbuff));
   %let dsname=%scan(&syspbuff,&num);
      proc print data=sasuser.&dsname;
      run;
   %end;
%mend printz;
If you submit a call to the macro that includes two parameters, the Printz macro writes the following line to the SAS log and causes two data sets to be printed
%printz(courses, schedule)
Table 11.7 SAS Log
Syspbuff contains: (courses,schedule)
If you submit a call to the macro that includes one parameter, the Printz macro writes the following line to the SAS log and causes one data set to be printed:
%printz(courses)
Table 11.8 SAS Log
Syspbuff contains: (courses)
Note: If the macro definition includes both a set of parameters and the PARMBUFF option, the macro invocation causes the parameters to receive values and the entire invocation list of values to be assigned to SYSPBUFF.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.158.151