BookmarkSubscribeRSS Feed
cjohnson
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;
run;
%mend mymacro;
Christopher Johnson
www.codeitmagazine.com
3 REPLIES 3
sbb
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 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.
Doc_Duke
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;
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.
Cynthia_sas
Diamond | Level 26
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.)

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 996 views
  • 0 likes
  • 4 in conversation