Ammonite | Level 13

## How to reverse the cumulative sum across the rows?

Hi Folks:

I have variables 'test' and 'negative' which are cumulatively added across rows. Could you please help me reverse the cumulative process? I'd like to have numbers for each row without encompassing the value in the preceding rows. I need the first 3 rows of 'test' variable 1,0,3 instead 1,1,4 before it's turned into cumulative sum across the rows?

``````data have;
length date \$10.;
input date \$ test negative;
cards;
1/20/2020	1	0
1/21/2020	1	0
1/22/2020	4	3
1/23/2020	22	21
1/24/2020	27	25
1/25/2020	27	25
1/26/2020	51	47
1/27/2020	61	56
1/28/2020	116	97
1/29/2020	187	155
1/30/2020	246	199
1/31/2020	312	245
2/1/2020	371	289
2/2/2020	429	327
2/3/2020	490	414
;``````

ac

1 ACCEPTED SOLUTION

Accepted Solutions
Opal | Level 21

## Re: How to reverse the cumulative sum across the rows?

Using DIF, and taking care of the first row:

``````
data want;
set have;
dtest = coalesce(dif(test), test);
dneg = coalesce(dif(negative), negative);
run;``````
PG
2 REPLIES 2
Super User

## Re: How to reverse the cumulative sum across the rows?

Look up the DIF() function.
Opal | Level 21

## Re: How to reverse the cumulative sum across the rows?

Using DIF, and taking care of the first row:

``````
data want;
set have;
dtest = coalesce(dif(test), test);
dneg = coalesce(dif(negative), negative);
run;``````
PG
Discussion stats
• 2 replies
• 1239 views
• 2 likes
• 3 in conversation