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!
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.
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.
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 :" ¶m1 ;
%mend ;
data _null_ ;
/* read Sample data and call macro passing a parameter */
set sample ;
%myMacro(string) ;
run ;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.