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

Hi SAS Users,

 

Could you help me figure out how to calculate duration of event dates and number of events based on a moving criteria? I would like to assign an event number based on any new event that occurs 30 days after the last grouping.  Below is my sample dataset, initial code and the wanted dataset. You can see from the wanted dataset, that once the duration exceeds 30 days, then a new event number is created and the count to 30 days starts over (e.g. subject=A1 date_event=03/19/2017).  I'm struggling with how to create the new events.

 

TIA,

Sophie

 

data have;

input SubjectID $3 date_event;

informat date_event mmddyy10.;

format date_event mmddyy10.;

datalines;

A1 10/12/2016

A1 11/05/2016

A1 11/06/2016

A1 03/19/2017

A1 04/01/2017

A1 08/08/2018

A1 09/01/2017

B2 12/10/2018

B2 12/14/2018

B2 01/03/2019

B2 06/20/2019

B2 07/02/2019

;;

run;

 

 

proc sql;

create table days30 as

select distinct a.SubjectID, a.date_event as date1, b.date_event as date2

from have as a

left join

have as b

on a.SubjectID=b.SubjectID

order by a.SubjectID, a.date_event

;

quit;

 

data days30;

set days30;

duration_event=intck('day',date1,date2);

run;

 

 

data want;

input SubjectID $ date_event duration_event no_event;

informat date_event mmddyy10.;

format date_event mmddyy10.;

datalines;

A1 10/12/2016 0 1

A1 11/05/2016 24 1

A1 11/06/2016 25 1

A1 03/19/2017 0 2

A1 04/01/2017 13 2

A1 08/08/2018 0 3

A1 09/01/2018 24 3

B2 12/10/2018 0 1

B2 12/14/2018 4 1

B2 01/03/2019 24 1

B2 06/20/2019 0 2

B2 07/02/2019 12 12

;;

run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @sophia_SAS 

 

 

data have;

input SubjectID :$3. date_event;

informat date_event mmddyy10.;

format date_event mmddyy10.;

datalines;
A1 10/12/2016
A1 11/05/2016
A1 11/06/2016
A1 03/19/2017
A1 04/01/2017
A1 08/08/2018
A1 09/01/2018
B2 12/10/2018
B2 12/14/2018
B2 01/03/2019
B2 06/20/2019
B2 07/02/2019
;;

run;

 
data want;
set have;
by subjectid;
retain t;
if first.subjectid then do; t=date_event; duration=0;no_event=1;end;
else do;
duration= ifn(intck('days',t,date_event)>30,0,intck('days',t,date_event));
if duration=0 then do;	no_event+1;	t= date_event;end;
end;
drop t;
run;

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

Hi @sophia_SAS 

 

 

data have;

input SubjectID :$3. date_event;

informat date_event mmddyy10.;

format date_event mmddyy10.;

datalines;
A1 10/12/2016
A1 11/05/2016
A1 11/06/2016
A1 03/19/2017
A1 04/01/2017
A1 08/08/2018
A1 09/01/2018
B2 12/10/2018
B2 12/14/2018
B2 01/03/2019
B2 06/20/2019
B2 07/02/2019
;;

run;

 
data want;
set have;
by subjectid;
retain t;
if first.subjectid then do; t=date_event; duration=0;no_event=1;end;
else do;
duration= ifn(intck('days',t,date_event)>30,0,intck('days',t,date_event));
if duration=0 then do;	no_event+1;	t= date_event;end;
end;
drop t;
run;
sophia_SAS
Obsidian | Level 7

Thank you! This is perfect! Definitely much better than any of my approaches.

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 754 views
  • 0 likes
  • 2 in conversation