BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

All, 
     I am trying to write a simple macro. The code is provided below and I would like to seek your help on what I could be doing wrong. 

%Macro Test(a,b,c,d,e,f); 

%Put &a; 
%Put &b; 
%Put &c; 
%Put &d;
%Put &e; 
%Put &f; 

Data _Null_;
	%Let Avg1 = Mean(&a,&b);
	 
	%Put &Avg1; 
	
	%Let Avg2 = Mean(&c,&d);
	
	%Put &Avg2; 
	
	%Let Res = (&Avg1 * &e) + (&Avg2 * &f); /* Code fails Here */ 
	
	&Res;
RUN;

%MEND;  

%Test(0.050,0.025,0.03,0.40,0.93,0.25);

Wonder what is tripping it. Would appreciate your guidance. 

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

It's not clear what you want for a final result. Do you want Res printed to the log or stored in a macro?

 

At any rate, think if the macro language as a "template" that enables you to write SAS code. As a beginner, start by writing the SAS code that you want, then use %LET statements to replace hard-coded values with macro values, and finally use the %MACRO and %MEND statements to encapsulate the logic.

 

The following statements print the result of Res to the log. You can use SYMPUT or SYMPUTX if you need that value in a macro variable.

 

%Macro Test(a,b,c,d,e,f); 
Data _Null_;
	Avg1 = Mean(&a,&b);
   Avg2 = Mean(&c,&d);
   Res = (Avg1 * &e) + (Avg2 * &f); /* Code fails Here */ 
	put Res;
RUN;

%MEND;  

%Test(0.050,0.025,0.03,0.40,0.93,0.25);

View solution in original post

7 REPLIES 7
Rick_SAS
SAS Super FREQ

It's not clear what you want for a final result. Do you want Res printed to the log or stored in a macro?

 

At any rate, think if the macro language as a "template" that enables you to write SAS code. As a beginner, start by writing the SAS code that you want, then use %LET statements to replace hard-coded values with macro values, and finally use the %MACRO and %MEND statements to encapsulate the logic.

 

The following statements print the result of Res to the log. You can use SYMPUT or SYMPUTX if you need that value in a macro variable.

 

%Macro Test(a,b,c,d,e,f); 
Data _Null_;
	Avg1 = Mean(&a,&b);
   Avg2 = Mean(&c,&d);
   Res = (Avg1 * &e) + (Avg2 * &f); /* Code fails Here */ 
	put Res;
RUN;

%MEND;  

%Test(0.050,0.025,0.03,0.40,0.93,0.25);
PeterClemmensen
Tourmaline | Level 20

&Res line should be 

 

%Put &Res;
PaigeMiller
Diamond | Level 26

Using macros is a very poor choice to do arithmetic. Macros don't like arithmetic operators like your *.

 

The whole thing would be better programmed in a data step.

--
Paige Miller
UdayGuntupalli
Quartz | Level 8

@PaigeMiller , 
      Can data steps be called repeatedly if needed ? For this macro, I tried to write I anticipate calling it repeatedly. So, I was not sure a data step can be called repeatedly in my code. If yes, can you show me an example of how to do what you are recommending and add demonstration around how the data step call can be repeated? 

PaigeMiller
Diamond | Level 26

First you need to explain what you are doing, and why you are doing it.

 

If nothing else, you could use CALL SYMPUTX in the data step to assign values to macro variables, and then you don't run into the problem you had with using an asterisk.

--
Paige Miller
UdayGuntupalli
Quartz | Level 8

@PaigeMiller
 I posted the exact code I am building. If you expect me to go into details about the problem I am trying to solve, I am afraid that's not something  I can do. You have the exact reference of a simple macro to answer the question with reference to your comments of why you are recommending the use of a data step. I believe that is sufficient to address the question at hand. 

PaigeMiller
Diamond | Level 26

The code posted by @Rick_SAS uses data step commands to do the arithmetic. It's simpler and doesn't have the problem you ran into.

 

But since you're apparently not going to explain further, there may be extremely simple ways to do this that you will never learn because you aren't going to explain.

--
Paige Miller

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1511 views
  • 7 likes
  • 4 in conversation