I have a data that keep track of the count of an event that occurs nonstop for different types of organisms. How can I populate the total number of nonstop occurrences for that event? So basically my data is like this
Sample | Date | Event |
1 | 1/1/2024 | 0 |
1 | 1/2/2024 | 0 |
1 | 1/3/2024 | 1 |
1 | 1/4/2024 | 2 |
1 | 1/5/2024 | 3 |
1 | 1/6/2024 | 0 |
1 | 1/7/2024 | 0 |
1 | 1/8/2024 | 1 |
1 | 1/9/2024 | 2 |
2 | 3/15/2024 | 1 |
2 | 3/16/2024 | 2 |
2 | 3/17/2024 | 3 |
2 | 3/18/2024 | 4 |
2 | 3/19/2024 | 5 |
2 | 3/20/2024 | 6 |
2 | 3/21/2024 | 7 |
2 | 3/22/2024 | 8 |
2 | 3/23/2024 | 9 |
3 | 5/10/2024 | 1 |
3 | 5/11/2024 | 2 |
3 | 5/12/2024 | 3 |
3 | 5/13/2024 | 4 |
3 | 5/14/2024 | 5 |
3 | 5/15/2024 | 0 |
3 | 5/16/2024 | 0 |
3 | 5/17/2024 | 0 |
3 | 5/18/2024 | 1 |
3 | 5/19/2024 | 2 |
3 | 5/20/2024 | 3 |
3 | 5/21/2024 | 4 |
3 | 5/22/2024 | 5 |
3 | 5/23/2024 | 6 |
3 | 5/24/2024 | 7 |
3 | 5/25/2024 | 0 |
3 | 5/26/2024 | 0 |
And I'd like to have output like this
Sample | Date | Event | Duration |
1 | 1/1/2024 | 0 | 0 |
1 | 1/2/2024 | 0 | 0 |
1 | 1/3/2024 | 1 | 3 |
1 | 1/4/2024 | 2 | 3 |
1 | 1/5/2024 | 3 | 3 |
1 | 1/6/2024 | 0 | 0 |
1 | 1/7/2024 | 0 | 0 |
1 | 1/8/2024 | 1 | 2 |
1 | 1/9/2024 | 2 | 2 |
2 | 3/15/2024 | 1 | 9 |
2 | 3/16/2024 | 2 | 9 |
2 | 3/17/2024 | 3 | 9 |
2 | 3/18/2024 | 4 | 9 |
2 | 3/19/2024 | 5 | 9 |
2 | 3/20/2024 | 6 | 9 |
2 | 3/21/2024 | 7 | 9 |
2 | 3/22/2024 | 8 | 9 |
2 | 3/23/2024 | 9 | 9 |
3 | 5/10/2024 | 1 | 5 |
3 | 5/11/2024 | 2 | 5 |
3 | 5/12/2024 | 3 | 5 |
3 | 5/13/2024 | 4 | 5 |
3 | 5/14/2024 | 5 | 5 |
3 | 5/15/2024 | 0 | 0 |
3 | 5/16/2024 | 0 | 0 |
3 | 5/17/2024 | 0 | 0 |
3 | 5/18/2024 | 1 | 7 |
3 | 5/19/2024 | 2 | 7 |
3 | 5/20/2024 | 3 | 7 |
3 | 5/21/2024 | 4 | 7 |
3 | 5/22/2024 | 5 | 7 |
3 | 5/23/2024 | 6 | 7 |
3 | 5/24/2024 | 7 | 7 |
3 | 5/25/2024 | 0 | 0 |
3 | 5/26/2024 | 0 | 0 |
Thank you!
data start_end; /* Determine start and end of event */
set have;
prev_date=lag(date);
prev_event=lag(event);
if date-prev_date>1 then new_event+1;
if event>0 and prev_event=0 then new_event+1;
if event=0 and prev_event>0 then new_event+1;
drop prev:;
run;
proc summary nway data=start_end; /* Determine duration of event */
class new_event;
var event;
output out=by_event max=duration;
run;
data final;
merge start_end by_event;
by new_event;
run;
From now on, please provide data as working SAS data step code (examples and instructions), and not as Excel files, and not as copy/paste from Excel, and not as screen captures.
data start_end; /* Determine start and end of event */
set have;
prev_date=lag(date);
prev_event=lag(event);
if date-prev_date>1 then new_event+1;
if event>0 and prev_event=0 then new_event+1;
if event=0 and prev_event>0 then new_event+1;
drop prev:;
run;
proc summary nway data=start_end; /* Determine duration of event */
class new_event;
var event;
output out=by_event max=duration;
run;
data final;
merge start_end by_event;
by new_event;
run;
From now on, please provide data as working SAS data step code (examples and instructions), and not as Excel files, and not as copy/paste from Excel, and not as screen captures.
It works perfectly. Thank you so much!
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.