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.

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