BookmarkSubscribeRSS Feed
roy1akash
Calcite | Level 5

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.

 

6 REPLIES 6
Reeza
Super User

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

 

novinosrin
Tourmaline | Level 20

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

BeverlyBrown
Amethyst | Level 16

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.
Astounding
PROC Star

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;

roy1akash
Calcite | Level 5

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.

 

 

Reeza
Super User

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

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 6 replies
  • 4113 views
  • 7 likes
  • 5 in conversation