BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lillymaginta1
Obsidian | Level 7
data have;
infile cards ;
input ID    first_Date last_Date ;	 
cards;
001 1/1/2004  1/5/2004
001 1/5/2004  1/10/2004
003 1/1/2005  4/5/2009
004 4/4/2010 4/5/2010
004 4/5/2010 4/15/2010
;

Hi everyone, I have the above data, I am trying to merge any intervals when the last date for that id equals the first date of a subsequent interval. For example for id number 1: I want the start date to be 1/1/2004 and the last date to be a/10/2004. Same for id number 4. 

output data

001 1/1/2004  1/10/2004
003 1/1/2005  4/5/2009
004 4/4/2010 4/15/2010
;
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

That is complicated . There too many scenario you need to consider.

 

data have;
infile cards ;
input ID    (first_Date last_Date) (: mmddyy10.) ;	 
cards;
001 1/1/2004  1/5/2004
001 1/5/2004  1/10/2004
003 1/1/2005  4/5/2009
004 4/4/2010 4/5/2010
004 4/5/2010 4/15/2010
;

data temp;
 set have;
 do date=first_date to last_date;
  output;
 end;
 drop first_date last_date;
run;
proc sort data=temp out=temp1 nodupkey;
by id date;
run;
data temp2;
 set temp1;
 by id;
 if first.id or dif(date) ne 1 then group+1;
run;
data want;
 do until(last.group);
  set temp2;
  by group;
  if first.group then first_date=date;
  if last.group then last_date=date;
 end;
  format first_date last_date mmddyy10.;
drop group date;
run;

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Does it always happen that the first_date of one record is always equal to the last_date for the previous record, within an ID? Or can there be cases where this is not true, for example:

 

001 1/1/2004  1/5/2004
001 1/6/2004  1/10/2004

 

--
Paige Miller
lillymaginta1
Obsidian | Level 7

Hi PaigeMiller, yes plenty. But in that case, I want to keep them as-is and only merge the one where the last date equals the first date of a subsequent record within the same ID. 

novinosrin
Tourmaline | Level 20

Hi @lillymaginta1  Please post a more comprehensive/representative sample of your HAVE and WANT that covers all your scenarios. Explain your requirements for those. That would help in providing a solution in one shot.

Ksharp
Super User

That is complicated . There too many scenario you need to consider.

 

data have;
infile cards ;
input ID    (first_Date last_Date) (: mmddyy10.) ;	 
cards;
001 1/1/2004  1/5/2004
001 1/5/2004  1/10/2004
003 1/1/2005  4/5/2009
004 4/4/2010 4/5/2010
004 4/5/2010 4/15/2010
;

data temp;
 set have;
 do date=first_date to last_date;
  output;
 end;
 drop first_date last_date;
run;
proc sort data=temp out=temp1 nodupkey;
by id date;
run;
data temp2;
 set temp1;
 by id;
 if first.id or dif(date) ne 1 then group+1;
run;
data want;
 do until(last.group);
  set temp2;
  by group;
  if first.group then first_date=date;
  if last.group then last_date=date;
 end;
  format first_date last_date mmddyy10.;
drop group date;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 795 views
  • 5 likes
  • 4 in conversation