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

I am trying to get the date when a cumulative sum exceeds a threshold (30 days here) by group, and only that row. I know I can add a cumulative sum output all the rows with > 30 and then sort and keep only the first row. But I am trying see if there is a way to do it in a single step.


data have;
input id date days ;
cards;
1 02DEC2014 3
1 01JAN2015 15
1 15JAN2015 15
2 02JAN2015 30
2 02FEB2015 30
2 01MAR2015 30
2 02APR2015 30
3 16DEC2014 3
3 30DEC2014 3
3 01JAN2015 3
3 16JAN2015 3
3 30JAN2015 3
3 01FEB2015 3
;

WANT:

1 15JAN2015
2 02FEB2015

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
data want;
set have;
retain found;
by id date;
if first.id then do ;cum_days = days; found=0; end;
else cum_days + days;

if cum_days > 30 and found = 0 then do;
found=1;
output;
end;

run;

Add another flag variable to check if the value was already output and use RETAIN to keep it the same across rows.

 


@nmp wrote:

I am trying to get the date when a cumulative sum exceeds a threshold (30 days here) by group, and only that row. I know I can add a cumulative sum output all the rows with > 30 and then sort and keep only the first row. But I am trying see if there is a way to do it in a single step.


data have;
input id date days ;
cards;
1 02DEC2014 3
1 01JAN2015 15
1 15JAN2015 15
2 02JAN2015 30
2 02FEB2015 30
2 01MAR2015 30
2 02APR2015 30
3 16DEC2014 3
3 30DEC2014 3
3 01JAN2015 3
3 16JAN2015 3
3 30JAN2015 3
3 01FEB2015 3
;

WANT:

1 15JAN2015
2 02FEB2015


FYI - please test the code you post runs correctly, your posted code did not run correctly for me. 
 

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Why does the WANT data set have only those two rows?

--
Paige Miller
nmp
Fluorite | Level 6 nmp
Fluorite | Level 6

Sorry, I just realized that my have data set may not have been clear. The days in the data set are not cumulative, so if I create a cumulative total it would look like the one below. The want data set has the dates when each person first had > 30 days. 1 has 33 on 15Jan2015, person 2 has 60 on 02Feb2015 and person 3 never has > 30, so doesn't show up in want.


data have;
input id visitdt days totdays;
cards;
1 02DEC2014 3 3
1 01JAN2015 15 18
1 15JAN2015 15 33
2 02JAN2015 30 30
2 02FEB2015 30 60
2 01MAR2015 30 90
2 02APR2015 30 120
3 16DEC2014 3 3
3 30DEC2014 3 6
3 01JAN2015 3 9
3 16JAN2015 3 12
3 30JAN2015 3 15
3 01FEB2015 3 18
;

Reeza
Super User
data want;
set have;
retain found;
by id date;
if first.id then do ;cum_days = days; found=0; end;
else cum_days + days;

if cum_days > 30 and found = 0 then do;
found=1;
output;
end;

run;

Add another flag variable to check if the value was already output and use RETAIN to keep it the same across rows.

 


@nmp wrote:

I am trying to get the date when a cumulative sum exceeds a threshold (30 days here) by group, and only that row. I know I can add a cumulative sum output all the rows with > 30 and then sort and keep only the first row. But I am trying see if there is a way to do it in a single step.


data have;
input id date days ;
cards;
1 02DEC2014 3
1 01JAN2015 15
1 15JAN2015 15
2 02JAN2015 30
2 02FEB2015 30
2 01MAR2015 30
2 02APR2015 30
3 16DEC2014 3
3 30DEC2014 3
3 01JAN2015 3
3 16JAN2015 3
3 30JAN2015 3
3 01FEB2015 3
;

WANT:

1 15JAN2015
2 02FEB2015


FYI - please test the code you post runs correctly, your posted code did not run correctly for me. 
 

nmp
Fluorite | Level 6 nmp
Fluorite | Level 6

Thank you for this simple, straightforward solution.

I will check my code in the future.

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!

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