SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Cruise
Ammonite | Level 13

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? 

 

Thanks for your time in advance. 

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
PGStats
Opal | Level 21

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

View solution in original post

2 REPLIES 2
Reeza
Super User
Look up the DIF() function.
PGStats
Opal | Level 21

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

sas-innovate-wordmark-2025-midnight.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. Sign up by March 14 for just $795.


Register now!

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
  • 2 replies
  • 1448 views
  • 2 likes
  • 3 in conversation