DATA Step, Macro, Functions and more

Calculating average across rows

Reply
New Contributor
Posts: 3

Calculating average across rows

[ Edited ]

I have a data set with the following structure:

 

Employees     Year_ID              Permanent                Salary         Average_Salary

                           -3                                                      6000 

                           -2                                                      6300

                           -1                                                      6500

A                          0                        1                            6600               ------

                            1                                                      6600

                            2                                                      6700

                            3                                                      6900

                           -3                                                      3900

                           -2                                                      4000

                           -1                                                      4050

                         0                       0                             4100              ------

                            1                                                      4100

                            2                                                      4250

                            3                                                      4300 

              and so on.

 

I have several thousand employees.

I need to compute the average salary of year (-3) and year(-2) when permanent = 1. Then I need to calculate salary of year(0) and year(1) when permanent = 0. The average values need to be on the row when Year_ID = 0.

 

Thank you so much for any help.

 

Super User
Posts: 23,980

Re: Please Help

Posted in reply to roy1akash

Welcome to the SAS forum.

 

Is the data posted what you have? If so, that looks like a report, so I assume you're using PROC REPORT or TABULATE. 

It would help if you could illustrate your source data, preferably in a data step and what you want as output and what you've tried so far. 

 

Additionally, for the following criteria, it doesn't look like you have any records that meet your condition. Please make sure your sample data aligns with your conditions. 

 

I need to compute the average salary of year (-3) and year(-2) when permanent = 1

 

Super User
Posts: 2,049

Re: Please Help

@Reeza can't be more right. Will attempt if requirements are clarified. 

 

My request to Reeza and @ChrisHemedinger  et al,  Can we have a link or some kind of demo for new community members showing them how to frame a question starting from subject line, a brief definition/description of the problem statement with samples and expected results while they register or perhaps a prompt soon as they register? Just my 2 cents. Thank you!

Community Manager
Posts: 700

Re: Please Help

Posted in reply to novinosrin

Hi @novinosrin, that info is readily available in here: How to Get Fast Helpful Answers.That article and others like it are available under Getting Started:

 

Getting Started on the SAS Support Communities.jpg

 

 

 

 

 

 

 

 

 

 

 

There are also help items in the menu under your profile (see below.) Might make sense to link them under Getting Started as well. 


Help in the SAS Support Communities.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Super User
Posts: 6,921

Re: Calculating average across rows

Posted in reply to roy1akash

I think this is what you are asking for:

 

data want;

set have end=done;

if done=0 then set have (keep=salary rename=(salary=forward1) firstobs=2);

back3 = lag3(salary);

back2 = lag2(salary);

if permanent=1 then average_salary = (back2 + back3) / 2;

else if permanent=0 then average_salary = (salary + forward1) / 2;

drop forward1 back2 back3;

run;

New Contributor
Posts: 3

Re: Calculating average across rows

Posted in reply to Astounding

Thank you so much for your response. I am extremely sorry I made a mistake in explaining the problem. I apologize for that.

Here is the correct explanation.

 

a) When Permanent = 1 and Year_ID = -3 then Average_Salary = Salary;

    When Permanent = 1 and Year_ID = -2 then Average_Salary = [Salary(Year_ID= -3)+Salary(Year_ID= -2)]/2;  

    When Permanent = 1 and Year_ID = -1 then Average_Salary = Salary; 

    When Permanent = 1 and Year_ID = -0 then Average_Salary = Salary;  

    When Permanent = 1 and Year_ID = 1 then Average_Salary = Salary;                

    When Permanent = 1 and Year_ID = 2 then Average_Salary = Salary;

    When Permanent = 1 and Year_ID = 3 then Average_Salary = Salary;

b) 

    When Permanent = 0 and Year_ID = -3 then Average_Salary = Salary;

    When Permanent = 0 and Year_ID = -2 then Average_Salary =  Salary;

    When Permanent = 0 and Year_ID = -1 then Average_Salary = Salary; 

    When Permanent = 0 and Year_ID = -0 then Average_Salary = Salary;  

    When Permanent = 0 and Year_ID = 1 then Average_Salary = [Salary(Year_ID= 0)+Salary(Year_ID= 1)]/2;                

    When Permanent = 0 and Year_ID = 2 then Average_Salary = Salary;

    When Permanent = 0 and Year_ID = 3 then Average_Salary = Salary;

 

Thank you.

 

 

Super User
Posts: 23,980

Re: Calculating average across rows

Posted in reply to roy1akash

https://stackoverflow.com/help/how-to-ask

 

Please review the suggestions in the link above on how to post a good question, and how to make a MCV example. 

https://stackoverflow.com/help/mcve

 

 

Ask a Question
Discussion stats
  • 6 replies
  • 347 views
  • 4 likes
  • 5 in conversation