Presentation is loading. Please wait.

Presentation is loading. Please wait.

3 Macro Parameters.

Similar presentations


Presentation on theme: "3 Macro Parameters."— Presentation transcript:

1 3 Macro Parameters

2 Define and call macros with parameters
Define and call macros with parameters. The difference between positional parameters and keyword parameters.

3 proc means data=orion.order_item &stats; var &vars; run; %mend calc;
%macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; Example: Note macro variable references within the CALC macro.

4 %let stats=min max; %let vars=quantity; %calc %let stats=n mean;
%let vars=discount; Example: Call the macro twice, each time with different values of the macro variables STATS and VARS. The user must submit three lines each time. How can this be simplified?

5 Macro Parameters %macro calc(stats,vars);
proc means data=mac1.order_item &stats; var &vars; run; %mend calc; Example: Define a macro with a parameter list of macro variables referenced within the macro.

6 Positional Parameters
%macro calc(stats,vars); proc means data=orion.order_item &stats; var &vars; run; %mend calc; %calc(min max,quantity) Positional parameters use a one-to-one correspondence between: parameter names supplied on the macro definition parameter values supplied on the macro call

7 Positional Parameters
%MACRO macro-name(parameter-1, … parameter-n); macro text %MEND <macro-name>; Parameter names are In Parentheses Comma delimited. Parameter values can be any text, null values, macro variable references, or macro calls.

8 Local Symbol Tables When a macro with a parameter list is called, the parameters are created in a separate local symbol table. The macro call %calc(min max, quantity) Global Table Local symbol tables are covered more thoroughly in chapter 5. SYSDAY Tuesday SYSLAST _NULL_ CITY Dallas AMOUNT 975 Local Table STATS min max VARS quantity

9 Local Symbol Tables A local symbol table is Created when a macro with a parameter list is called Deleted when the macro finishes execution. Macro variables in the local table are available only during macro execution and can be referenced only within the macro. Local symbol tables are short-lived.

10 Positional Parameters
%macro count(opts, start, stop); proc freq data=orion.orders; where order_date between "&start"d and "&stop"d; table order_type / &opts; title1 "Orders from &start to &stop"; run; %mend count; options mprint; %count(nocum,01jan2004,31dec2004) %count(,01jul2004,31dec2004) title; In the second call, a comma is used as a placeholder for the OPTS parameter, which receives a null value. A null value after a slash is not a problem for the PROC FREQ TABLE statement.

11 proc catalog cat=work.sasmacr;
contents; title "My Temporary Macros"; quit; title;

12 Keyword Parameters

13 Keyword Parameters %MACRO macro-name(keyword=value, …, keyword=value);
macro text %MEND <macro-name>; Keyword parameters are assigned a default value after an equal (=) sign. The main advantage of keyword parameters is that default values can be assigned. If you omit a keyword parameter from a macro call, that parameter will receive whatever default value was specified on the macro definition.

14 Keyword Parameters %macro-name(keyword=value, …, keyword=value)
keyword=value combinations can be specified in any order omitted from the call without placeholders. If omitted from the call, a keyword parameter receives its default value.

15 Keyword Parameters %macro count(opts=,start=01jan04,stop=31dec04);
proc freq data=orion.orders; where order_date between "&start"d and "&stop"d; table order_type / &opts; title1 "Orders from &start to &stop"; run; %mend count; options mprint; %count(opts=nocum) %count(stop=01jul04,opts=nocum nopercent) %count() title; The first parameter has a null value by default. On the final call, all parameters receive their default value. The empty parens are important because this macro "knows" it has a parameter list. If you omit the parens, the macro will not execute but patiently await its expected parameter list. If the next token submitted does not begin a parameter list, the macro will "know" that a parameter list is not forthcoming and will execute using default parameter values. Parens, even if empty, are recommended as explicit, unambiguous, and guarantee immediate execution of the macro.

16 Mixed Parameter Lists You can use a combination of positional and keyword parameters. In a mixed parameter list, positional parameters must be listed before keyword parameters in both the macro definition and the macro call.

17 Mixed Parameter Lists Example: Use a combination of positional and keyword parameters. %macro count(opts,start=01jan04,stop=31dec04); proc freq data=orion.orders; where order_date between "&start"d and "&stop"d; table order_type / &opts; title1 "Orders from &start to &stop"; run; %mend count; options mprint; %count(nocum) %count(stop=30jun04,start=01apr04) %count(nocum nopercent,stop=30jun04) %count() title;


Download ppt "3 Macro Parameters."

Similar presentations


Ads by Google