DATA Step, Macro, Functions and more

What is the syntax for using a do loop to dynamically call a macro?

Accepted Solution Solved
Reply
Valued Guide
Posts: 583
Accepted Solution

What is the syntax for using a do loop to dynamically call a macro?

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;


Accepted Solutions
Solution
3 weeks ago
Super User
Super User
Posts: 9,193

Re: What is the syntax for using a do loop to dynamically call a macro?

Posted in reply to DavidPhillips2

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


All Replies
Super User
Super User
Posts: 9,193

Re: What is the syntax for using a do loop to dynamically call a macro?

Posted in reply to DavidPhillips2

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.

Valued Guide
Posts: 583

Re: What is the syntax for using a do loop to dynamically call a macro?

[ Edited ]

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

%body(college1);

%body(college2);

Solution
3 weeks ago
Super User
Super User
Posts: 9,193

Re: What is the syntax for using a do loop to dynamically call a macro?

Posted in reply to DavidPhillips2

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;
Valued Guide
Posts: 583

Re: What is the syntax for using a do loop to dynamically call a macro?

[ Edited ]

 

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;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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