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
SAS Super FREQ
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.)

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 766 views
  • 0 likes
  • 4 in conversation