<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: finding time periods that overlap in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/finding-time-periods-that-overlap/m-p/472462#M121149</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dlm=' ' truncover;
  input id start_date:mmddyy. end_date:mmddyy.;
  format start_date end_date date9.;
  datalines;
1 3/1/2016 6/2/2016
1 4/1/2016 7/1/2016
1 8/2/2016 12/1/2016
1 1/2/2017 3/1/2017
2 5/10/2014 12/1/2014
2 7/10/2014 10/1/2014
2 1/1/2015 3/2/2015
;
run;
data temp;
 set have;
 by id;
 if start_date &amp;lt;= lag(end_date) then flag=1;
 if first.id then flag=.;
run;
data temp;
 set temp;
 if missing(flag) then group+1;
run;
proc sql;
create table want as
 select *
  from temp
   group by group
    having sum(flag) &amp;gt; 0;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 22 Jun 2018 12:56:48 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2018-06-22T12:56:48Z</dc:date>
    <item>
      <title>finding time periods that overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-time-periods-that-overlap/m-p/472350#M121114</link>
      <description>&lt;P&gt;Hello all!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset in which I would like to be able to find time periods that overlap for each id.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is an example of the start dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id &amp;nbsp; &amp;nbsp; &amp;nbsp;start_date &amp;nbsp; &amp;nbsp; &amp;nbsp; end_date&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 3/1/2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6/2/2016&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 4/1/2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7/1/2016&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 8/2/2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12/1/2016&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 1/2/2017 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3/1/2017&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 5/10/2014 &amp;nbsp; &amp;nbsp; &amp;nbsp;12/1/2014&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 7/10/2014 &amp;nbsp; &amp;nbsp; &amp;nbsp;10/1/2014&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 1/1/2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3/2/2015&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to be able to identify the rows for each id with the overlapping periods, like below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id &amp;nbsp; &amp;nbsp; &amp;nbsp;start_date &amp;nbsp; &amp;nbsp; &amp;nbsp; end_date&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 3/1/2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6/2/2016&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 4/1/2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7/1/2016&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 5/10/2014 &amp;nbsp; &amp;nbsp; &amp;nbsp;12/1/2014&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 7/10/2014 &amp;nbsp; &amp;nbsp; &amp;nbsp;10/1/2014&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone guide me on this?&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jun 2018 01:52:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-time-periods-that-overlap/m-p/472350#M121114</guid>
      <dc:creator>SarahW13</dc:creator>
      <dc:date>2018-06-22T01:52:33Z</dc:date>
    </item>
    <item>
      <title>Re: finding time periods that overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-time-periods-that-overlap/m-p/472351#M121115</link>
      <description>&lt;P&gt;there is discussion of overlap here: &lt;A href="http://www2.sas.com/proceedings/sugi31/048-31.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi31/048-31.pdf&lt;/A&gt;&lt;/P&gt;&lt;P&gt;they use arrays&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jun 2018 02:08:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-time-periods-that-overlap/m-p/472351#M121115</guid>
      <dc:creator>pau13rown</dc:creator>
      <dc:date>2018-06-22T02:08:44Z</dc:date>
    </item>
    <item>
      <title>Re: finding time periods that overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-time-periods-that-overlap/m-p/472359#M121119</link>
      <description>&lt;P&gt;A SQL self-join makes this sort of problem often quite simple.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dlm=' ' truncover;
  input id start_date:mmddyy. end_date:mmddyy.;
  format start_date end_date date9.;
  datalines;
1 3/1/2016 6/2/2016
1 4/1/2016 7/1/2016
1 8/2/2016 12/1/2016
1 1/2/2017 3/1/2017
2 5/10/2014 12/1/2014
2 7/10/2014 10/1/2014
2 1/1/2015 3/2/2015
;
run;

proc sql;&lt;BR /&gt;  create table want as
  select 
    l.*
  from have l, have r
    where 
      /* test only within same id */
  l.id=r.id
  /* do not join a record with itself */
  and not (l.start_date=r.start_date and l.end_date=r.end_date)
  /* find overlapping dates */
  and 
  (
  l.start_date between r.start_date and r.end_date
  or
  r.start_date between l.start_date and l.end_date
    )
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Jun 2018 03:18:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-time-periods-that-overlap/m-p/472359#M121119</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-06-22T03:18:37Z</dc:date>
    </item>
    <item>
      <title>Re: finding time periods that overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-time-periods-that-overlap/m-p/472462#M121149</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dlm=' ' truncover;
  input id start_date:mmddyy. end_date:mmddyy.;
  format start_date end_date date9.;
  datalines;
1 3/1/2016 6/2/2016
1 4/1/2016 7/1/2016
1 8/2/2016 12/1/2016
1 1/2/2017 3/1/2017
2 5/10/2014 12/1/2014
2 7/10/2014 10/1/2014
2 1/1/2015 3/2/2015
;
run;
data temp;
 set have;
 by id;
 if start_date &amp;lt;= lag(end_date) then flag=1;
 if first.id then flag=.;
run;
data temp;
 set temp;
 if missing(flag) then group+1;
run;
proc sql;
create table want as
 select *
  from temp
   group by group
    having sum(flag) &amp;gt; 0;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Jun 2018 12:56:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-time-periods-that-overlap/m-p/472462#M121149</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-06-22T12:56:48Z</dc:date>
    </item>
  </channel>
</rss>

