What is the syntax for using a do loop to dynamically call a macro? I am trying to select a list and for each item in the list call a macro that accepts one element from the list at a time.
E.g.
proc sql;
select college from school_list;
quit;
for each college;
%body(college);
run;
It would be whatever you wanted it to be. What call execute does it take a string, and put that string into the execution run after the datastep finishes. Hence anything you can create as a string, be it macro calls, base code anything, can be pushed out. For instance, if you want the word college and an incremental number:
data _null_; do i=1 to 2; call execute(cats('%body(',put(i,best.),');')); end; run;
The simplest way is to use a datastep:
data _null_; set school_list; call execute(cats('%body(',college,');')); run;
Although in most cases, the really simple method is to use by groups rather than writing macro code.
In this example what is being sent to the macro? Would it function like:
%body(college1);
%body(college2);
It would be whatever you wanted it to be. What call execute does it take a string, and put that string into the execution run after the datastep finishes. Hence anything you can create as a string, be it macro calls, base code anything, can be pushed out. For instance, if you want the word college and an incremental number:
data _null_; do i=1 to 2; call execute(cats('%body(',put(i,best.),');')); end; run;
This worked for me.
proc sql;
create table departmentList as
select distinct department from enrollment;
quit;
data _null_;
set departmentList;
call execute(cats('%callStudentBody(',department,');'));
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.