DATA Step, Macro, Functions and more

macro parameter

Reply
Occasional Contributor
Posts: 5

macro parameter

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?

Super User
Posts: 19,789

Re: macro parameter

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;

Super User
Super User
Posts: 7,949

Re: macro parameter

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;

Super User
Posts: 19,789

Re: macro parameter

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.

Regular Contributor
Posts: 227

Re: macro parameter

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

Regular Contributor
Posts: 227

Re: macro parameter

Posted in reply to Ron_Fehd_macro_maven

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.

Super User
Super User
Posts: 7,949

Re: macro parameter

Posted in reply to Ron_Fehd_macro_maven

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.

Ask a Question
Discussion stats
  • 6 replies
  • 292 views
  • 0 likes
  • 4 in conversation