BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
danielela
Calcite | Level 5
Hi all,

I have a table like this:

Section | start time | end time
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


What I want to do is to check all overlapping and create a new column to label each row. Like this:

Section | start time | end time | group
Aaa | 01Jun2025:00:01:10 | 01Jun2025:00:09:02 | 1
Aaa | 01Jun2025:00:03:10 | 01Jun2025:00:12:00 | 2 (start time is earlier than previous end time, so label as 2)
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)
Bbb | 01Jun2025:00:01:02 | 01Jun2025:00:03:01 | 1 ( new section restart the calculation from group 1)
Bbb | 01Jun2025:00:02:10 | 01Jun2025:00:09:08 |2


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.

Any idea?
1 ACCEPTED SOLUTION

Accepted Solutions
LinusH
Tourmaline | Level 20

Checking interval can be a bit tricky, but I don't think this is an application for macro.
Have the data set sorted by section and start time, then do a data-set-by.
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.

Data never sleeps

View solution in original post

3 REPLIES 3
LinusH
Tourmaline | Level 20

Checking interval can be a bit tricky, but I don't think this is an application for macro.
Have the data set sorted by section and start time, then do a data-set-by.
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.

Data never sleeps
danielela
Calcite | Level 5
Thanks!
Tom
Super User Tom
Super User

First let's convert your example listing into an actual dataset so we have something to code against.

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
;

Sound like you want to remember the END timestamp from the first of the record in a  block.  You can use a new retained variable to do that.

data want;
  set have;
  by section start;
  retain first_end;
  format first_end datetime19.;
  if first.section or (start > first_end) then do;
    group=1; first_end=end;
  end;
  else group+1;
run;

Results:

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 899 views
  • 0 likes
  • 3 in conversation