BookmarkSubscribeRSS Feed
DoumbiaS
Quartz | Level 8

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

 

9 REPLIES 9
Kurt_Bremser
Super User

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
DoumbiaS
Quartz | Level 8

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;

Kurt_Bremser
Super User

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.

DoumbiaS
Quartz | Level 8

Thanks,

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

DoumbiaS
Quartz | Level 8

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

Kurt_Bremser
Super User

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.

ballardw
Super User

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?

 

 

DoumbiaS
Quartz | Level 8

No i don't thinnk so.

Your explanations are so clear, thanks for that.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 1115 views
  • 4 likes
  • 4 in conversation