BookmarkSubscribeRSS Feed
Obsidian | Level 7
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;
%mend mymacro;
Christopher Johnson
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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 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

Scott Barry
SBBWorks, Inc.
Rhodochrosite | Level 12
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;
%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.
And this is also a succinct overview: (see step 6 and step 7 for examples of positional vs keyword parameters)


(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.)


Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 4 in conversation