DATA Step, Macro, Functions and more

multiple execution of macro with parameters stored in dataset

Reply
Contributor
Posts: 60

multiple execution of macro with parameters stored in dataset

I need to execute  macro with parameters many times.

Parameters for subsequent executions are stored in data set.

Is there a simple way (other than CALL EXECUTE) to go through data set and execute macro with proper parameters?

I tried  CALL EXECUTE but this method seems non-deterministic - macro itself executes but from CALL EXECUTE returns many errors.

Super User
Posts: 19,772

Re: multiple execution of macro with parameters stored in dataset

Well you could nest your macros and call it that way, but that's what call execute is designed for.

Super User
Posts: 5,498

Re: multiple execution of macro with parameters stored in dataset

Tom,

CALL EXECUTE will be tricky, because the macro statements that it generates execute immediately.  Only the SAS language statements wait for the current DATA step to finish.

You could try either of two methods.  CALL EXECUTE will probably work if you generate a slightly different set of code.  Instead of generating %MYMAC (parameters), generate %nrstr(%MYMAC (parameters)).  That will probably do the trick.  But if not, you could go back to the old-fashioned way.  Have the DATA step that currently uses CALL EXECUTE write out the macro calls to a file using FILE and PUT statements.  Then %include the file after the DATA step is over.

Good luck.

Contributor
Posts: 60

Re: multiple execution of macro with parameters stored in dataset

Posted in reply to Astounding

I tried %nrstr but macro istself executes ok but with call execute returns still errors.

I have some call symput statements in macros 2 levels under which according to documentation are not allowed. I tried to put variable as PROC SQL; SELECT A INTO :A FROM X but still call execute seems indeterministic.

I thought about concatenating parameters (to paramters column) and also adding _N_ column with data step. Then I'll try executing inside %do %while statement as %my_macro(&parameters) getting &paramteres from proper record with proc sql;

If someone has any other ideas to do that without CALL EXECUTE I'll appreciate help.

Super User
Posts: 5,498

Re: multiple execution of macro with parameters stored in dataset

Tom,

Well, you'll have to show us some of the error messages if you would like help debugging it.

CALL SYMPUT is definitely allowed within a macro definition.  (It's a CARDS statement that will give you an error.)  The tricky issue with CALL SYMPUT inside a macro definition is figuring out which symbol table is being used.  But it's too early to say whether that would be a problem for your program.

And it is still possible to switch to the FILE / PUT statement approach.  At least that will make it clearer which errors are generated by the DATA step that now contains the CALL EXECUTEs, vs. which are caused by executing the macros.

Contributor
Posts: 60

Re: multiple execution of macro with parameters stored in dataset

Posted in reply to Astounding

Ok. I'll put error messages as soon as I have access to code at work..

Generally the problem is that macro produces errors that some macro variables couldn't be resolved and some other variables resolve to eg. "n inestead of n (when executed normally) - i use some proc sql statements inside macro called by call execute. I use some where conditions based on macro variables.

I also concatenate values so generally mess with " and ' might be an issue here - correct me if i'm wrong.

But still macro executes ok itself and from call execute executes in a different way - i need to get code that i trust and is deterministic.

Trusted Advisor
Posts: 1,301

Re: multiple execution of macro with parameters stored in dataset

if you want to use call execute use %nrstr to have it execute after the datastep completes.

data args;

a = 1; b = 11; c = "one  "; output;

a = 2; b = 22; c = "two  "; output;

a = 3; b = 33; c = "three"; output;

run;

%macro mymacro( a , b , c );

%put &c : a=&a b=&b;

%mend;

data _null_;

set args;

  call execute('%nrstr(%mymacro(' || a || ',' || b || ',' || c || '));');

run;

NOTE: CALL EXECUTE generated line.

1         +  %mymacro(           1,          11,one  );

one : a=1 b=11

2         +  %mymacro(           2,          22,two  );

two : a=2 b=22

3         +  %mymacro(           3,          33,three);

three : a=3 b=33

You could also build an include file instead:

filename x temp;

data _null_;

set args;

file x;

put '%mymacro(' a ',' b ',' c ');';

run;

%include "%sysfunc(pathname(x))" /source2;

Regular Contributor
Posts: 241

Re: multiple execution of macro with parameters stored in dataset

I do this often. As long as your macro behaves, this should work. hth

  data args;
    a=1; b=11; c="one  "; output;
    a=2; b=22; c="two  "; output;
    a=3; b=33; c="three"; output;
  run;

  %macro doOne(a, b, c);
    %put a=&a b=&b c=&c;
  %mend  doOne;

  %macro doAll(data=);
    %local dsid rc OK a b c;
    %let OK = 0;
    %let dsid = %sysfunc(open(&data,i));
    %syscall set(dsid);
    %do %while (%sysfunc(fetch(&dsid))=&OK);

      %*;%doOne(&a, &b, &c)

    %end;
    %let rc = %sysfunc(close(&dsid));
  %mend  doAll;

  %*-- check --*;
  %doAll(data=args)
  %*-- on log
  a=1 b=11 c=one
  a=2 b=22 c=two
  a=3 b=33 c=three
  --*;

Super User
Posts: 10,020

Re: multiple execution of macro with parameters stored in dataset

The only thing I can think is using macro variable matrix .

data args;
a = 1; b = 11; c = "one  "; output;
a = 2; b = 22; c = "two  "; output;
a = 3; b = 33; c = "three"; output;
run;

%macro mymacro( a , b , c );
%put &c : a=&a b=&b;
%mend;


proc sql noprint;
select count(*) into : n from args;
select a,b,c
   into : a1 - : a%left(&n) ,
    : b1 - : b%left(&n) ,
    : c1 - : c%left(&n) from args;
quit;

%macro execute;
%do i=1 %to &n;
 %mymacro(&&a&i,&&b&i, &&c&i )
%end;
%mend execute;

%execute


Ksharp

Super User
Super User
Posts: 7,039

Re: multiple execution of macro with parameters stored in dataset

I find that using a data step to generate the code is the easiest.  You can generate code. Look at it. Run a few lines. Until you get it working.  Then run it for real using %INCLUDE.

Example:

filename code temp;

data _null_;

  set sashelp.class;

  put '%mymacro(' name ',' sex ')' ;

run;

%include code / source2;

Ask a Question
Discussion stats
  • 9 replies
  • 345 views
  • 1 like
  • 7 in conversation