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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

DavidPhillips2
Rhodochrosite | Level 12

In this example what is being sent to the macro?  Would it function like:

%body(college1);

%body(college2);

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
DavidPhillips2
Rhodochrosite | Level 12

 

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 942 views
  • 0 likes
  • 2 in conversation