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.
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.
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
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!
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.
Ready to level-up your skills? Choose your own adventure.