## Mean Calculation in Data Step

Solved
Occasional Contributor
Posts: 18

# Mean Calculation in Data Step

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.

Accepted Solutions
Solution
3 weeks ago
SAS Super FREQ
Posts: 4,171

## Re: Mean Calculation in Data Step

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);``````

All Replies
Solution
3 weeks ago
SAS Super FREQ
Posts: 4,171

## Re: Mean Calculation in Data Step

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);``````
PROC Star
Posts: 1,215

## Re: Mean Calculation in Data Step

&Res line should be

``%Put &Res;``
Posts: 2,825

## Re: Mean Calculation in Data Step

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
Occasional Contributor
Posts: 18

## Re: Mean Calculation in Data Step

@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?

Posts: 2,825

## Re: Mean Calculation in Data Step

[ Edited ]

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
Occasional Contributor
Posts: 18

## Re: Mean Calculation in Data Step

@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.

Posts: 2,825

## Re: Mean Calculation in Data Step

[ Edited ]

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
☑ This topic is solved.

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