This is how I solve this problem in my day to day programming, when it is necessary to call a macro repeatedly based on data. This approach works equally well with many datasets or many variables from one dataset or many different macro calls from one dataset - whichever it is, simply create a dataset with the information that varies and call it this way.
This approach combines elements of Shorack's solution with user2337871's and Neil's. Why do it differently?
- Macro should be called with parameters, not contain its parameter definitions inside of it. That makes it more flexible for future use (where for example the
dataset_list
dataset might be something different).
- Having flexibility to call based on dataset of names rather than requiring macro to call macro
- Removing code into a macro (instead of inside the
call execute
or other calling method) makes it easier to read.
call execute
may have some drawbacks depending on the manipulation you are doing (related to macro variable timing)
Let's say you are doing a PROC MEANS and then appending that to a master dataset. While this is actually a very slow and annoying way to do that (as opposed to combining them together and using BY, or even using ODS OUTPUT with noncombined datasets), we'll assume your actual task is more complicated.
%macro do_my_stuff(dataset=);
proc means data=&dataset noprint;
var count;
output out=dsn_&dataset. mean=;
run;
proc append base=results data=dsn_&dataset. force;
run;
%mend do_my_Stuff;
proc sql;
select cats('%do_my_stuff(dataset=',name,')') into :stufflist separated by ' '
from dictionary.tables
where memname='WORK';
quit;
&stufflist;
You can add additional criteria to the where statement in the proc sql, or call that using CALL EXECUTE, or a number of different options. You can also use a self-made dataset with the dataset names (and even the variables as another column and macro parameter, if the variable(s) of interest vary by dataset).