<?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: checking for overlapping datetime in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/checking-for-overlapping-datetime/m-p/973166#M46133</link>
    <description>&lt;P&gt;First let's convert your example listing into an actual dataset so we have something to code against.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input section $ (start end) (:datetime.);
  format start end datetime19.;
cards;
Aaa  01Jun2025:00:01:10  01Jun2025:00:09:02
Aaa  01Jun2025:00:03:10  01Jun2025:00:12:00
Aaa  01Jun2025:00:10:09  01Jun2025:00:18:08
Bbb  01Jun2025:00:01:02  01Jun2025:00:03:01
Bbb  01Jun2025:00:02:10  01Jun2025:00:09:08
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Sound like you want to remember the END timestamp from the first of the record in a&amp;nbsp; block.&amp;nbsp; You can use a new retained variable to do that.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by section start;
  retain first_end;
  format first_end datetime19.;
  if first.section or (start &amp;gt; first_end) then do;
    group=1; first_end=end;
  end;
  else group+1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs section               start                 end           first_end group

 1    Aaa    01JUN2025:00:01:10  01JUN2025:00:09:02  01JUN2025:00:09:02   1
 2    Aaa    01JUN2025:00:03:10  01JUN2025:00:12:00  01JUN2025:00:09:02   2
 3    Aaa    01JUN2025:00:10:09  01JUN2025:00:18:08  01JUN2025:00:18:08   1
 4    Bbb    01JUN2025:00:01:02  01JUN2025:00:03:01  01JUN2025:00:03:01   1
 5    Bbb    01JUN2025:00:02:10  01JUN2025:00:09:08  01JUN2025:00:03:01   2
&lt;/PRE&gt;</description>
    <pubDate>Sat, 23 Aug 2025 15:20:27 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2025-08-23T15:20:27Z</dc:date>
    <item>
      <title>checking for overlapping datetime</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/checking-for-overlapping-datetime/m-p/969274#M46009</link>
      <description>Hi all,&lt;BR /&gt;&lt;BR /&gt;I have a table like this:&lt;BR /&gt;&lt;BR /&gt;Section | start time | end time&lt;BR /&gt;Aaa | 01Jun2025:00:01:10 | 01Jun2025:00:09:02&lt;BR /&gt;Aaa | 01Jun2025:00:03:10 | 01Jun2025:00:12:00&lt;BR /&gt;Aaa | 01Jun2025:00:10:09 | 01Jun2025:00:18:08&lt;BR /&gt;Bbb | 01Jun2025:00:01:02 | 01Jun2025:00:03:01&lt;BR /&gt;Bbb | 01Jun2025:00:02:10 | 01Jun2025:00:09:08&lt;BR /&gt;…&lt;BR /&gt;&lt;BR /&gt;What I want to do is to check all overlapping and create a new column to label each row. Like this:&lt;BR /&gt;&lt;BR /&gt;Section | start time | end time | group&lt;BR /&gt;Aaa | 01Jun2025:00:01:10 | 01Jun2025:00:09:02 | 1&lt;BR /&gt;Aaa | 01Jun2025:00:03:10 | 01Jun2025:00:12:00 | 2 (start time is earlier than previous end time, so label as 2)&lt;BR /&gt;Aaa | 01Jun2025:00:10:09 | 01Jun2025:00:18:08 | 1 (start time is later than group 1 end time, so group 1 is released and can assign to this row)&lt;BR /&gt;Bbb | 01Jun2025:00:01:02 | 01Jun2025:00:03:01 | 1 ( new section restart the calculation from group 1)&lt;BR /&gt;Bbb | 01Jun2025:00:02:10 | 01Jun2025:00:09:08 |2&lt;BR /&gt;…&lt;BR /&gt;&lt;BR /&gt;I tried to loop through all row, and record all “group” latest end time in macro variable. Then compare each row endtime with each variable. But it takes so slow.&lt;BR /&gt;&lt;BR /&gt;Any idea?&lt;BR /&gt;</description>
      <pubDate>Wed, 18 Jun 2025 06:53:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/checking-for-overlapping-datetime/m-p/969274#M46009</guid>
      <dc:creator>danielela</dc:creator>
      <dc:date>2025-06-18T06:53:43Z</dc:date>
    </item>
    <item>
      <title>Re: checking for overlapping datetime</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/checking-for-overlapping-datetime/m-p/969279#M46010</link>
      <description>&lt;P&gt;Checking interval can be a bit tricky, but I don't think this is an application for macro.&lt;BR /&gt;Have the data set sorted by section and start time, then do a data-set-by.&lt;BR /&gt;Create two retained columns for start and end time, so you can compare those values with current start and end time on the next record - and based on that comparison set your label.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Aug 2025 10:28:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/checking-for-overlapping-datetime/m-p/969279#M46010</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2025-08-27T10:28:53Z</dc:date>
    </item>
    <item>
      <title>Re: checking for overlapping datetime</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/checking-for-overlapping-datetime/m-p/973161#M46132</link>
      <description>Thanks!&lt;BR /&gt;</description>
      <pubDate>Sat, 23 Aug 2025 12:34:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/checking-for-overlapping-datetime/m-p/973161#M46132</guid>
      <dc:creator>danielela</dc:creator>
      <dc:date>2025-08-23T12:34:15Z</dc:date>
    </item>
    <item>
      <title>Re: checking for overlapping datetime</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/checking-for-overlapping-datetime/m-p/973166#M46133</link>
      <description>&lt;P&gt;First let's convert your example listing into an actual dataset so we have something to code against.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input section $ (start end) (:datetime.);
  format start end datetime19.;
cards;
Aaa  01Jun2025:00:01:10  01Jun2025:00:09:02
Aaa  01Jun2025:00:03:10  01Jun2025:00:12:00
Aaa  01Jun2025:00:10:09  01Jun2025:00:18:08
Bbb  01Jun2025:00:01:02  01Jun2025:00:03:01
Bbb  01Jun2025:00:02:10  01Jun2025:00:09:08
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Sound like you want to remember the END timestamp from the first of the record in a&amp;nbsp; block.&amp;nbsp; You can use a new retained variable to do that.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by section start;
  retain first_end;
  format first_end datetime19.;
  if first.section or (start &amp;gt; first_end) then do;
    group=1; first_end=end;
  end;
  else group+1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs section               start                 end           first_end group

 1    Aaa    01JUN2025:00:01:10  01JUN2025:00:09:02  01JUN2025:00:09:02   1
 2    Aaa    01JUN2025:00:03:10  01JUN2025:00:12:00  01JUN2025:00:09:02   2
 3    Aaa    01JUN2025:00:10:09  01JUN2025:00:18:08  01JUN2025:00:18:08   1
 4    Bbb    01JUN2025:00:01:02  01JUN2025:00:03:01  01JUN2025:00:03:01   1
 5    Bbb    01JUN2025:00:02:10  01JUN2025:00:09:08  01JUN2025:00:03:01   2
&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Aug 2025 15:20:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/checking-for-overlapping-datetime/m-p/973166#M46133</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-08-23T15:20:27Z</dc:date>
    </item>
  </channel>
</rss>

