05-15-2012 01:28 PM
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.
05-15-2012 02:11 PM
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.
05-15-2012 02:22 PM
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(¶meters) getting ¶mteres from proper record with proc sql;
If someone has any other ideas to do that without CALL EXECUTE I'll appreciate help.
05-15-2012 02:42 PM
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.
05-15-2012 04:36 PM
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.
05-15-2012 05:16 PM
if you want to use call execute use %nrstr to have it execute after the datastep completes.
a = 1; b = 11; c = "one "; output;
a = 2; b = 22; c = "two "; output;
a = 3; b = 33; c = "three"; output;
%macro mymacro( a , b , c );
%put &c : a=&a b=&b;
call execute('%nrstr(%mymacro(' || a || ',' || b || ',' || c || '));');
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;
put '%mymacro(' a ',' b ',' c ');';
%include "%sysfunc(pathname(x))" /source2;
05-15-2012 04:50 PM
I do this often. As long as your macro behaves, this should work. hth
a=1; b=11; c="one "; output;
a=2; b=22; c="two "; output;
a=3; b=33; c="three"; output;
%macro doOne(a, b, c);
%put a=&a b=&b c=&c;
%local dsid rc OK a b c;
%let OK = 0;
%let dsid = %sysfunc(open(&data,i));
%do %while (%sysfunc(fetch(&dsid))=&OK);
%*;%doOne(&a, &b, &c)
%let rc = %sysfunc(close(&dsid));
%*-- check --*;
%*-- on log
a=1 b=11 c=one
a=2 b=22 c=two
a=3 b=33 c=three
05-15-2012 10:11 PM
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
05-15-2012 10:44 PM
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.
filename code temp;
put '%mymacro(' name ',' sex ')' ;
%include code / source2;