BookmarkSubscribeRSS Feed
dapenDaniel
Obsidian | Level 7

Hi,

 

I have a question about the rolling sum. Suppose I have the data below.

data have;
input firmID $	Year	Value;
datalines;
A	1995	1
A	1996	2
A	1997	3
A	1998	4
A	1999	5
A	2000	6
A	2001	7
A	2002	8
A	2003	9
B	1995	10
B	1996	11
B	1997	12
B	1998	13
B	1999	14
B	2000	15
B	2001	16
B	2002	17
B	2003	18
;
run;

The code below provided by @PGStats works well.

proc expand data=have out=want;
by firmid;
id Year;
Convert value=cul_value / transformout=(movsum 3 trimleft 2);
run;

The output is below.

firmIDYearValueCul_value
A19951.
A19962.
A199736
A199849
A1999512
A2000615
A2001718
A2002821
A2003924
B199510.
B199611.
B19971233
B19981336
B19991439
B20001542
B20011645
B20021748
B20031851

 

However, I want that the sum exclude the last value. For example, Cul_value is 6 (1+2+3) for firm A in 1998 rather than 9. The expected output is below.

 

firmIDYearValuecul_want
A19951.
A19962.
A19973.
A199846
A199959
A2000612
A2001715
A2002818
A2003921
B199510.
B199611.
B199712.
B19981333
B19991436
B20001539
B20011642
B20021745
B20031848

 

How can I revise the program? Alternatively, what new program do I need to use? Thanks.

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

Here is a fix for your currrent code:

 

proc expand data=have out=want;
   by firmid;
   id Year;
   Convert value=cul_value / transformout=(-1 movsum 3 trimleft 3);
run;

Result:

 

firmID Year  cul_value  Value 
A      1995  .          1 
A      1996  .          2 
A      1997  .          3 
A      1998  6          4 
A      1999  9          5 
A      2000  12         6 
A      2001  15         7 
A      2002  18         8 
A      2003  21         9 
B      1995  .          10 
B      1996  .          11 
B      1997  .          12 
B      1998  33         13 
B      1999  36         14 
B      2000  39         15 
B      2001  42         16 
B      2002  45         17 
B      2003  48         18 
dapenDaniel
Obsidian | Level 7

Hi @PeterClemmensen , I revised the code based on your advice but the output is not what I expected. Sorry, the data above is just an example and the difference between values in the real data is not always 1. There are also some zeros in the real dataset, which lead the output to be negative.

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

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
  • 3 replies
  • 820 views
  • 1 like
  • 2 in conversation