BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lillymaginta
Lapis Lazuli | Level 10
data a;
  input patientid  start1_date : mmddyy10. end1_date : mmddyy10. start2 : mmddyy10.  end2 : mmddyy10. ;
  format  start1_date end1_date start2 end2  mmddyy10.;
datalines;
1 5/5/2009 6/6/2009  1/1/2006 2/2/2006
1 5/5/2009 6/6/2009  2/2/2007 3/2/2007
1 5/5/2009 6/6/2009 5/1/2009 6/1/2009
1 5/5/2009 6/6/2009 7/7/2009 12/1/2009;
run;

I have the above data. I am trying to create a subset that includes all periods for (start2-end2) that overlap with any of the intervals from (start1_date- end1_date). No requirement for a minimum overlap in days. 

Required output for this data

1 5/5/2009 6/6/2009 5/1/2009 6/1/2009

 

1 ACCEPTED SOLUTION

Accepted Solutions
mansour_ib_sas
Pyrite | Level 9

Hello,

I propose this solution:

data a;
  input patientid  start1_date : mmddyy10. end1_date : mmddyy10. start2 : mmddyy10.  end2 : mmddyy10. ;
  format  start1_date end1_date start2 end2  mmddyy10.;
datalines;
1 5/5/2009 6/6/2009  1/1/2006 2/2/2006
1 5/5/2009 6/6/2009  2/2/2007 3/2/2007
1 5/5/2009 6/6/2009 5/1/2009 6/1/2009
1 5/5/2009 6/6/2009 7/7/2009 12/1/2009
;
run;

data b;
do until (last.patientid);
set a;
by patientid;
if intnx('month',start1_date,0,"BEGINNING")=intnx('month',start2,0,"BEGINNING")
and 
intnx('month',end1_date,0,"BEGINNING")=intnx('month',end2,0,"BEGINNING")
then output;
end;

run;

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

Hi @lillymaginta  While I always enjoyed attempting to solve your questions, this one is not quite clear. I m wary of my understanding of the requirement.

Anyways:

 

data a;
  input patientid  start1_date : mmddyy10. end1_date : mmddyy10. start2 : mmddyy10.  end2 : mmddyy10. ;
  format  start1_date end1_date start2 end2  mmddyy10.;
datalines;
1 5/5/2009 6/6/2009  1/1/2006 2/2/2006
1 5/5/2009 6/6/2009  2/2/2007 3/2/2007
1 5/5/2009 6/6/2009 5/1/2009 6/1/2009
1 5/5/2009 6/6/2009 7/7/2009 12/1/2009
;
run;

proc sql;
create table want(drop=x:) as
select *,abs (start2-start1_date) as x1,abs(end2-end1_date) as x2
from a
group by patientid
having min(abs (start2-start1_date))=x1 and min(abs(end2-end1_date))=x2
order by patientid,start2,end2;
quit;
mansour_ib_sas
Pyrite | Level 9

Hello,

I propose this solution:

data a;
  input patientid  start1_date : mmddyy10. end1_date : mmddyy10. start2 : mmddyy10.  end2 : mmddyy10. ;
  format  start1_date end1_date start2 end2  mmddyy10.;
datalines;
1 5/5/2009 6/6/2009  1/1/2006 2/2/2006
1 5/5/2009 6/6/2009  2/2/2007 3/2/2007
1 5/5/2009 6/6/2009 5/1/2009 6/1/2009
1 5/5/2009 6/6/2009 7/7/2009 12/1/2009
;
run;

data b;
do until (last.patientid);
set a;
by patientid;
if intnx('month',start1_date,0,"BEGINNING")=intnx('month',start2,0,"BEGINNING")
and 
intnx('month',end1_date,0,"BEGINNING")=intnx('month',end2,0,"BEGINNING")
then output;
end;

run;
lillymaginta
Lapis Lazuli | Level 10

Hi 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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