DATA Step, Macro, Functions and more

MACRO VARIABLES - VARIABLE LIST

Reply
Frequent Contributor
Posts: 97

MACRO VARIABLES - VARIABLE LIST

I am trying to build a macro to handle a sequence of PROC's, but the BY variables and the VAR variables may change. Can you pass lists of variables to a macro to use in the BY and VAR statements?

i.e. something like:

%mymacro(mydataset, ('var1', 'var2'), ('var3', 'var4'));

%macro mymacro(dataset=,list1=, list2=);
proc means sum data=&dataset;
by &list1;
var &list2;
run;
%mend mymacro;
Christopher Johnson
www.codeitmagazine.com
Super Contributor
Super Contributor
Posts: 3,174

Re: MACRO VARIABLES - VARIABLE LIST

SAS macro language programming supports both positional and keyword-driven macro variable specification when invoking a SAS macro. Your example shared shows incompatible references, the macro invocation showing positional and the %MACRO statement definition showing keyword. These two references must be consistent.

Suggest using keyword-drive for self-documentation. Also, suggest reviewing SAS support http://support.sas.com/ website references, both SAS-hosted product documentation on Macro Language, and supplemental technical / conference papers. Either use the website SEARCH facility or consider the Google advanced search argument below:

sas macro programming site:sas.com


Scott Barry
SBBWorks, Inc.
Trusted Advisor
Posts: 2,115

Re: MACRO VARIABLES - VARIABLE LIST

Sure, but you've got too many quotes and parentheses.

One way:

%macro mymacro(dataset,list1, list2);
proc means sum data=&dataset;
by &list1;
var &list2;
run;
%mend mymacro;

%mymacro(mydataset, var1 var2, var3 var4);

The Book-by-User "Professional SAS Programming Shortcuts" has a lot more on how to implement macros for various situations and is a bit easier to learn from than the Macro Reference Manual.
SAS Super FREQ
Posts: 8,864

Re: MACRO VARIABLES - VARIABLE LIST

And this is also a succinct overview:
http://www2.sas.com/proceedings/sugi28/056-28.pdf (see step 6 and step 7 for examples of positional vs keyword parameters)

cynthia

(FWIW, whenever I coded production macros, the standard was -always- to use keyword and not positional parameters. It's easier to maintain, easier to invoke and less confusing for a non-macro programmer to figure out.)
Ask a Question
Discussion stats
  • 3 replies
  • 132 views
  • 0 likes
  • 4 in conversation