BookmarkSubscribeRSS Feed
googa22
Calcite | Level 5

I have a sas table which contains paths from my computer. how can I use the table column as parameter for a macro which I need to run for every path?

6 REPLIES 6
Reeza
Super User

Call execute

/********************************************************************

Example : Call macro using parameters from data set

********************************************************************/

proc sort data=sashelp.class out=class;

by age sex;

run;

%macro summary(age=, sex=);

proc print data=sashelp.class;

where age=&age and sex="&sex";

run;

%mend;

data sample;

set class;

by age sex;

if last.sex;

string =

  catt('%summary(age=', age, ',sex=', sex, ');');

put string;

run;

data _null_;

set sample;

call execute(string);

run;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

But then why bother with the macro at all:

data _null_;

     set sashelp.class;

     call execute('proc print data=sashelp.class;

                               where age='||strip(put(age,best.))||' and sex="'||strip(sex)|||'";

                            run;');

run;

Reeza
Super User

That is a stock example for call execute and passing parameters, I would assume that the actual macro the OP is calling is more complicated.

Ron_MacroMaven
Lapis Lazuli | Level 10

this page contains a macro which reads a data set and calls another macro

using values in each row as parameters for the called macro.

http://www.sascommunity.org/wiki/Macro_CallMacr

Ron_MacroMaven
Lapis Lazuli | Level 10

beware of using call execute to generate a macro call without enclosing the macro call in %nrstr

i.e.

call execute(catt('%nrstr(%MyMacro(data=',data,',var=',var

,'))'

));

(did I count and quote all the parentheses correctly?)

Why: this will save your soul when you get around to using this trick

to call a macro which contains complexity: %if %do or symput.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, that is actually to do with the order of the compilation phase.  If you call execute a macro with a call symput in, then it only affects the first occurence.   So if you doing that multiple times in a macro then it will work, but will only contain the first value.  Generally I avoid using macro and call execute, mainly because pretty much anything I do in macro can be done in call execute or vice versa.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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