Passing Information into a Macro Using Parameters

You have seen the basic form for a macro definition. Your macros will 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 create or call the macro.

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.
Syntax, macro definition that includes positional parameters:
%MACRO macro-name(parameter-1<,...,parameter-n>);
text
%MEND <macro-name>;
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 following statements are true about the values listed in a macro call:
  • They can be null values, text, macro variable references, or macro calls.
  • They are assigned to the parameter variables using a one-to-one correspondence.

Example: Using Positional Parameters to Create Macro Variables

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 Certadv.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(certadv.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.
Syntax, macro definition that includes keyword parameters:
%MACRO macro-name(keyword-1=<value-1><,...,keyword-n=<value-n>>);
text
%MEND <macro-name>;
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, in any order. 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: Using Keyword Parameters to Create Macro Variables

You can use keyword parameters to create the macro variables Dsn and Vars in the Printdsn macro. This example assigns a default value of Certadv.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=certadv.courses,
               vars=
course_code course_title days);
   proc print data=&dsn;
      var &vars;
   title "Listing of %upcase(&dsn) data set";
   run;
%mend;
To call the Printdsn macro with a value of Certadv.Schedule for Dsn and a value of teacher course_code begin_date for Vars, issue the following call:
%printdsn(vars=teacher course_code begin_date, dsn=certadv.schedule)
To call the Printdsn macro with default values for the parameters (Certadv.Courses as the value for Dsn and course_code course_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=certadv.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.
Syntax, macro definition that includes mixed parameters:
%MACRO macro-name(parameter-1<,...,parameter-n>,
keyword-1=<value-1><,...,keyword-n=<value-n>>);
text
%MEND macro-name;
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: Using Mixed Parameters to Create Macro Variables

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 Certadv.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(certadv.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 Certadv.Courses to the macro variable Dsn. You could issue the following call:
%printdsn(certadv.courses)
Because this call omits the keyword parameter (Vars), the default value for that parameter is used.
Last updated: October 16, 2019
..................Content has been hidden....................

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