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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 700 views
  • 0 likes
  • 2 in conversation