Calcite | Level 5

## Calculating average across rows

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

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!

Community Manager

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:

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

SAS Innovate 2025 call for content is open through Mon., Sept. 16. Submit your presentation idea today!

PROC Star

## Re: Calculating average across rows

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;

Calcite | Level 5

## Re: Calculating average across rows

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

## Re: Calculating average across rows

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

Discussion stats
• 6 replies
• 3432 views
• 7 likes
• 5 in conversation