<?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: How to find overlap between different dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-overlap-between-different-dates/m-p/752381#M236990</link>
    <description>&lt;P&gt;Thanks for setting up the data for ready code testing.&amp;nbsp; This program produces what you want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have (keep=id);
  by id notsorted;
  merge have  
        have (firstobs=2 keep=event_start_date rename=(event_start_date=_nxt_evt));

  if last.id=0 and _nxt_evt&amp;lt;event_end_date
        or
     first.id=0 and event_start_date&amp;lt;lag(event_end_date);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The program assumes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Data are grouped by ID.&lt;/LI&gt;
&lt;LI&gt;Data are sorted by event_start_date within each ID.&lt;/LI&gt;
&lt;LI&gt;&lt;EM&gt;&lt;STRONG&gt;Overlaps are examined only for consecutive records.&lt;/STRONG&gt;&lt;/EM&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Number 3 is important because if you had three records, in which the 2nd and 3rd records don't overlap each other, but both overlap or are contained in the first, this code would not include the third record.&amp;nbsp; Is that condition possible in your data?&lt;/P&gt;</description>
    <pubDate>Tue, 06 Jul 2021 18:31:27 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2021-07-06T18:31:27Z</dc:date>
    <item>
      <title>How to find overlap between different dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-overlap-between-different-dates/m-p/752320#M236954</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hi Community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help to get only overlapping records.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input EVENT_ID 6. EVENT_START_DATE DATETIME19. EVENT_END_DATE DATETIME19. ID 2.;
FORMAT EVENT_START_DATE EVENT_END_DATE DATETIME19. ;
datalines;
048110 10Jun2021:13:00:00 10Jun2021:14:00:00 6
049076 10Jun2021:14:00:00 10Jun2021:15:00:00 6
026365 20Sep2019:14:00:00 21Sep2019:18:00:00 9
026361 20Sep2019:16:30:00 20Sep2019:19:00:00 9
037522 25Sep2020:13:00:00 26Sep2020:11:30:00 9
035367 25Sep2020:16:30:00 25Sep2020:20:00:00 9
027322 20Sep2019:13:00:00 20Sep2019:17:00:00 2
026361 20Sep2019:16:30:00 20Sep2019:19:00:00 2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;For id 6 wanted to remove those as there is no overlap between first event_start_date and second event_start_date or event_date. For id 2 there is half an hour overlap between two events (event&amp;nbsp;027322 started at 20Sep2019 13:00:00 and ended at&amp;nbsp;20Sep2019 17:00:00 and for same id started second event before first event ended(over lap of half an hour) .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Want :&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="440"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="116"&gt;EVENT_ID&lt;/TD&gt;
&lt;TD width="133"&gt;EVENT_START_DATE&lt;/TD&gt;
&lt;TD width="127"&gt;EVENT_END_DATE&lt;/TD&gt;
&lt;TD width="64"&gt;ID&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;026365&lt;/TD&gt;
&lt;TD&gt;20Sep2019 14:00:00&lt;/TD&gt;
&lt;TD&gt;21Sep2019 18:00:00&lt;/TD&gt;
&lt;TD&gt;9&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;026361&lt;/TD&gt;
&lt;TD&gt;20Sep2019 16:30:00&lt;/TD&gt;
&lt;TD&gt;20Sep2019 19:00:00&lt;/TD&gt;
&lt;TD&gt;9&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;037522&lt;/TD&gt;
&lt;TD&gt;25Sep2020 13:00:00&lt;/TD&gt;
&lt;TD&gt;26Sep2020 11:30:00&lt;/TD&gt;
&lt;TD&gt;9&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;035367&lt;/TD&gt;
&lt;TD&gt;25Sep2020 16:30:00&lt;/TD&gt;
&lt;TD&gt;25Sep2020 20:00:00&lt;/TD&gt;
&lt;TD&gt;9&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;027322&lt;/TD&gt;
&lt;TD&gt;20Sep2019 13:00:00&lt;/TD&gt;
&lt;TD&gt;20Sep2019 17:00:00&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;026361&lt;/TD&gt;
&lt;TD&gt;20Sep2019 16:30:00&lt;/TD&gt;
&lt;TD&gt;20Sep2019 19:00:00&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Tue, 06 Jul 2021 16:30:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-overlap-between-different-dates/m-p/752320#M236954</guid>
      <dc:creator>Siva_Harish</dc:creator>
      <dc:date>2021-07-06T16:30:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to find overlap between different dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-overlap-between-different-dates/m-p/752344#M236965</link>
      <description>&lt;P&gt;Please show what your desired output dataset should look like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And please provide your available data in a SAS data step format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jul 2021 15:42:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-overlap-between-different-dates/m-p/752344#M236965</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-07-06T15:42:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to find overlap between different dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-overlap-between-different-dates/m-p/752352#M236971</link>
      <description>Hi ,&lt;BR /&gt;&lt;BR /&gt;i had modified my post .Can you help me now?</description>
      <pubDate>Tue, 06 Jul 2021 16:31:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-overlap-between-different-dates/m-p/752352#M236971</guid>
      <dc:creator>Siva_Harish</dc:creator>
      <dc:date>2021-07-06T16:31:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to find overlap between different dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-overlap-between-different-dates/m-p/752381#M236990</link>
      <description>&lt;P&gt;Thanks for setting up the data for ready code testing.&amp;nbsp; This program produces what you want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have (keep=id);
  by id notsorted;
  merge have  
        have (firstobs=2 keep=event_start_date rename=(event_start_date=_nxt_evt));

  if last.id=0 and _nxt_evt&amp;lt;event_end_date
        or
     first.id=0 and event_start_date&amp;lt;lag(event_end_date);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The program assumes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Data are grouped by ID.&lt;/LI&gt;
&lt;LI&gt;Data are sorted by event_start_date within each ID.&lt;/LI&gt;
&lt;LI&gt;&lt;EM&gt;&lt;STRONG&gt;Overlaps are examined only for consecutive records.&lt;/STRONG&gt;&lt;/EM&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Number 3 is important because if you had three records, in which the 2nd and 3rd records don't overlap each other, but both overlap or are contained in the first, this code would not include the third record.&amp;nbsp; Is that condition possible in your data?&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jul 2021 18:31:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-overlap-between-different-dates/m-p/752381#M236990</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-07-06T18:31:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to find overlap between different dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-overlap-between-different-dates/m-p/752504#M237055</link>
      <description>&lt;P&gt;A slightly different take, using the basically same "look ahead" technique:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
merge
  have
  have (
    firstobs=2
    keep=id event_start_date
    rename=(id=_id event_start_date=_start)
  )
;
if
  id = lag(id) and event_start_date &amp;lt; lag(event_end_date)
  or
  id = _id and event_end_date &amp;gt; _start
;
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;if the dataset is sorted by id and event_start_date (or at least sorted by date within a id group), then an overlap in the third (or later) obs would force an overlap in the previous obs.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jul 2021 09:24:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-overlap-between-different-dates/m-p/752504#M237055</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-07-07T09:24:11Z</dc:date>
    </item>
  </channel>
</rss>

