BookmarkSubscribeRSS Feed
victory
Fluorite | Level 6

Dear all,

I want to calculate the average of value from 1-7 if year is in 2021/12.  And if year is in 2020/12, and calculate the average of value from 8-14.  Add an additional column to display the data corresponding to the average value of each period, for example, each row of 1-7 displays the average value of 1-7 at the end, each row of 8-14 displays the average value of 8-14 at the end.  How can i edit the code.

victory_0-1671173922245.png

Thanks all.

4 REPLIES 4
andreas_lds
Jade | Level 19

No need to use an if statement, just use proc summary and merge the result back to the dataset you have. Or use a dow-loop.

victory
Fluorite | Level 6
How can i edit the code. I've never written code like this. Will it be complicated?
andreas_lds
Jade | Level 19

How complex a task is depends largely on the experience one has. Luckily you don't have to reinvent the wheel, just search for papers dealing with merge back summary statistics. Adapting the code should not be to difficult.

PaigeMiller
Diamond | Level 26

May I ask what you will do with this additional column that contains the average? Are you trying to find out how far above or below the average the value of variable VALUE is? Or do you have some other reason for doing this, if so, what? (Really, explain the reason why you want this; explaining the reason should always be a part of your original post, we shouldn't have to ask)

 

If you are trying to find out how far above or below the average the value of variable VALUE is, then use PROC STDIZE, no extra column needed.

 

proc stdize data=have method=mean out=want;
    var value;
    by year;
run;

 

Also, we have in the past asked you to not provide data as screen captures, but as working SAS data step code. Please do that in the future.

--
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!

Discussion stats
  • 4 replies
  • 3287 views
  • 1 like
  • 3 in conversation