BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tmcwill
Fluorite | Level 6

I created a SAS macro, lets call it mymacro, that takes a single string as input. I have a list of about 30 strings (with no pattern) that I need to run the macro for. I thought I could somehow put the strings in an array and then create another macro that loops the original macro over each item in the array, but I can't find information on how to create an array outside of a data step. 

 

For example: 

List of input to mymacro  : foo, world, dog, cat, tree, ...

 

Need a more efficient way of doing: 

%mymacro(foo)

%mymacro(world)

%mymacro(dog)

...

 

I'm sure there is a simple way of doing this, but I'm fairly new to macros in SAS. Thanks in advance! 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

If the "list" is space delimited I might suggest a macro "driver" to call your macro:

 

%macro driver (parmlist);
%do i=1 %to %sysfunc(countw(&parmlist.));
   %let word = %scan(&parmlist.,&i.);
   %mymacro (&word.);
%end;
%mend;

%driver (<your list goes here>)

DO not use this with a comma delimited list as commas are default macro parameter separators and causes issues.

If you have used a specific character to delimit your strings then it would go in the third position of %scan above. You may want to check the documentation for %scan to get the default list of delimiters.

 

 

View solution in original post

3 REPLIES 3
ballardw
Super User

If the "list" is space delimited I might suggest a macro "driver" to call your macro:

 

%macro driver (parmlist);
%do i=1 %to %sysfunc(countw(&parmlist.));
   %let word = %scan(&parmlist.,&i.);
   %mymacro (&word.);
%end;
%mend;

%driver (<your list goes here>)

DO not use this with a comma delimited list as commas are default macro parameter separators and causes issues.

If you have used a specific character to delimit your strings then it would go in the third position of %scan above. You may want to check the documentation for %scan to get the default list of delimiters.

 

 

tmcwill
Fluorite | Level 6
My list was space delimited, so this was exactly what I needed. Thank you!
AMSAS
SAS Super FREQ

If your list is in a SAS dataset then you could do this:

options mlogic symbolgen ;
/* Create Sample Data */
data sample ;
	infile cards ;
	input string:$12. ;
cards;
This
is
my
test
data 
;

/* Sample Macro */
%macro myMacro(param1) ;
	put "param1 :" &param1 ;
%mend ;

data _null_ ;
	/* read Sample data and call macro passing a parameter */
	set sample ;
	%myMacro(string) ;
run ;
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
  • 1306 views
  • 0 likes
  • 3 in conversation