DATA Step, Macro, Functions and more

Call Execute

Reply
Contributor
Posts: 49

Call Execute

Hello,

 

One can not invoke a macro using Call execute in oder to handle a created global macro var by call sypmut.

How is that ? Any minds is welcome.

 

Regards

 

Trusted Advisor
Posts: 1,612

Re: Call Execute

Super User
Posts: 6,936

Re: Call Execute

Post your code.

 

This works:

%macro mymac(varname);
%put Variable as received by macro: &&&varname;
%mend;

data _null_;
call symputx('myvar','mytext','g');
call execute('%mymac(myvar)');
run;

Log from that:

24         %macro mymac(varname);
25         %put Variable as received by macro: &&&varname;
26         %mend;
27         
28         data _null_;
29         call symputx('myvar','mytext','g');
30         call execute('%mymac(myvar)');
31         run;

Variable as received by macro: mytext
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 49

Re: Call Execute

Look at this example: val_m2 values are not updated form call execute.

Regards

 

data have;
do i=1 to 10;
	have= i+1;
	output;
end;
run;

%macro mymacro(nc);
%Global used mg1 mg2;
%let mg1= 20; %let mg2= 40;
data need; set have nobs= nobs; J= _n_;
var0= J * &mg1 / &mg2;     				
var1= var0 + 0.5 * &mg1 / &mg2;
var2= var0 + var1;
nexp= var2 * &mg1 / &mg2;
need + (((nexp - have)**2) / nexp);           /*cumulative sum*/ 
if J= nobs then	call symput('used',need); 
run;
%mend;

data see;
do i= 1 to 3;
	if i= 1 then do;
		max= 40;  min= 15;  m1= 27.5; 
		val_m1= 2;  m2= 33.75;   
		call execute('%mymacro('||m2||')');
		val_m2= input(symget('used'), 30.);    /*updating*/              
	end;
	else do;
		min= m1;  m1= m2;  val_m1= val_m2;  m2= (m2 + max)/2;   /*updating*/		
		call execute('%mymacro('||m2||')');
		val_m2 = input(symget('used'), 30.);
	end;
output;
end;
run;

Super User
Posts: 6,936

Re: Call Execute

The data step in the macro can only be compiled and run after the data step with the call execute has finished. Macro code in a macro that is call executed, OTOH, will be resolved immediately when pushed into the SAS interpreter queue.

 

If you want to use data from a second dataset dynamically, but in a way that can not be handled by a join, consider using a hash object.

 

Since you only use an overall statistic from dataset have, I'd prepare that beforehand. No need to reread the whole dataset in every iteration.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 49

Re: Call Execute

Thanks,

An overall statistics is not used. When the macro execute, it use the current variable have value.

Contributor
Posts: 49

Re: Call Execute

Also, the macro executes with the last m2 value as argument.

Super User
Posts: 6,936

Re: Call Execute

Since dataset 'have' never changes, and only the results of the final iteration of the data step are put into the macro variable, and the code in the macro is not dynamic at all, there is no need to call the macro at all. Just set &need once.

 

If you plan on doing something completely different, show THAT code.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 10,497

Re: Call Execute

Where you use these two lines in the data step:

call execute('%mymacro('||m2||')');  <= creates a call to the macro that executes AFTER the data step
val_m2= input(symget('used'), 30.);  <= so the value of USED is not changed prior to this line. It will be the same value for every iteration

 

Do you intentionally through away and decimal part of used?

 

 

Contributor
Posts: 49

Re: Call Execute

No i don't thinnk so.

Your explanations are so clear, thanks for that.

 

Ask a Question
Discussion stats
  • 9 replies
  • 119 views
  • 4 likes
  • 4 in conversation