BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dapenDaniel
Obsidian | Level 7

Hi,

 

I have a panel dataset like below.

data have;
input FirmID $ Year Count;
datalines;
1001        1992          1
1001        1993          2
1001        1994          3
1001        1995          4
1001        1996          5
1001        1997          5
1002        1992          6
1002        1993          7
1002        1994          8
1002        1995          9
1002        1996          8
1002        1997          7
;

 

I would like to get the sum of the count in the previous 3 years. For example, Pre3 in 1995 is 6 (1+2+3). The result I want is below.

 

FirmID      Year       Count       Pre3

1001        1992          1             .

1001        1993          2             .

1001        1994          3             .

1001        1995          4            6

1001        1996          5            9

1001        1997          5           12

1002        1992          6            .
1002        1993          7            .
1002        1994          8           .
1002        1995          9          21
1002        1996          8          24
1002        1997          7          25

What program do I need to use?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Just add a little by-group processing to the solution for https://communities.sas.com/t5/New-SAS-User/Sum-values-in-lagged-years/m-p/623759:

data want;
set have;
by firmid;
if first.firmid
then l_count = 1;
else l_count + 1;
l_count1 = lag(count);
l_count2 = lag2(count);
l_count3 = lag3(count);
if l_count gt 3 then pre3 = l_count1 + l_count2 + l_count3;
drop l_:;
run;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Just add a little by-group processing to the solution for https://communities.sas.com/t5/New-SAS-User/Sum-values-in-lagged-years/m-p/623759:

data want;
set have;
by firmid;
if first.firmid
then l_count = 1;
else l_count + 1;
l_count1 = lag(count);
l_count2 = lag2(count);
l_count3 = lag3(count);
if l_count gt 3 then pre3 = l_count1 + l_count2 + l_count3;
drop l_:;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 264 views
  • 0 likes
  • 2 in conversation