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

My question is about the conditional cumulative sum in SAS. I think it can be explained better by using sample. I have following dataset:

Date                 Value
01/01/2001          10
02/01/2001          20
03/01/2001          30
04/01/2001          15
05/01/2001          25
06/01/2001          35
07/01/2001          20
08/01/2001          45
09/01/2001          35

I want to find the cumulative sum of value. My condition is if cumulative sum more than 70, it should be 70 and the next cumulative sum should be began from the excessive value over 70 and so on.. More preciesly, my new data should be:

Date                 Value    Cumulative 
01/01/2001           10        10
02/01/2001           20        30
03/01/2001           30        60
04/01/2001           15        70
05/01/2001           25        30 ( 75-70=5+25=30)
06/01/2001           35        65
07/01/2001           20        70
08/01/2001           45        60  ( 85-70=15+45=60)
09/01/2001           35        95   ( because its last value)

Many thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

One simple way:

 

data have;
input Date :mmddyy10. Value;
format date mmddyy10.;
datalines;
01/01/2001          10
02/01/2001          20
03/01/2001          30
04/01/2001          15
05/01/2001          25
06/01/2001          35
07/01/2001          20
08/01/2001          45
09/01/2001          35
;

data want;
set have end=done;
cumulative + value;
if cumulative > 70 and not done then do;
    remainder = cumulative - 70;
    cumulative = 70;
    output;
    cumulative = remainder;
    end;
else output;
drop remainder;
run;

proc print; run;
PG

View solution in original post

5 REPLIES 5
Reeza
Super User

What do you have so far?

 

I don't know if this is you as well, but it's an almost identical question:

http://stackoverflow.com/questions/41556209/conditional-cumulative-sum-in-sas

 

Khaladdin
Quartz | Level 8

Hi Reeza,

 

I have seen this post.The problem is, it is not the same post. In my case, I want to cumulative sum equal to the value that I select as my condition. And the next cumulative sum should be the sum of excessive value over cumulative sum and next value. More detailed, in that case ( as said in link, it is 10):

5     5                                                                                                                                                                                                       3     8                                                                                                                                                                                                       4     4   ( because 12>10)                                                                                                                                                                         3     7   

But in my case it should be:                                                                                                                                                                     

5     5                                                                                                                                                                                                       3     8                                                                                                                                                                                                       4    10                                                                                                                                                                                                       3     5    ( 12-10=2+3=5)     

PGStats
Opal | Level 21

One simple way:

 

data have;
input Date :mmddyy10. Value;
format date mmddyy10.;
datalines;
01/01/2001          10
02/01/2001          20
03/01/2001          30
04/01/2001          15
05/01/2001          25
06/01/2001          35
07/01/2001          20
08/01/2001          45
09/01/2001          35
;

data want;
set have end=done;
cumulative + value;
if cumulative > 70 and not done then do;
    remainder = cumulative - 70;
    cumulative = 70;
    output;
    cumulative = remainder;
    end;
else output;
drop remainder;
run;

proc print; run;
PG
DanielSantos
Barite | Level 11

Hi.

 

Here's another way of doing it, just using algebra.

 

data want;
     set have end=done;

     drop _:;
     _cumulative + value; * sum value;
     cumulative=min(70+(done*_cumulative),_cumulative); * minimize to 70 or not if done;
     _cumulative=_cumulative-int(cumulative/70)*70; * subtract if > 70;
run;

 

 

min function will return the minimum value and int will returns the integer part.

 

Hope it helps.

 

Daniel Santos @ www.cgd.pt

Khaladdin
Quartz | Level 8

Many thanks.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 2752 views
  • 2 likes
  • 4 in conversation