BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Amelia6
Obsidian | Level 7

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

SampleDateEvent
11/1/20240
11/2/20240
11/3/20241
11/4/20242
11/5/20243
11/6/20240
11/7/20240
11/8/20241
11/9/20242
23/15/20241
23/16/20242
23/17/20243
23/18/20244
23/19/20245
23/20/20246
23/21/20247
23/22/20248
23/23/20249
35/10/20241
35/11/20242
35/12/20243
35/13/20244
35/14/20245
35/15/20240
35/16/20240
35/17/20240
35/18/20241
35/19/20242
35/20/20243
35/21/20244
35/22/20245
35/23/20246
35/24/20247
35/25/20240
35/26/20240

And I'd like to have output like this

SampleDateEventDuration
11/1/202400
11/2/202400
11/3/202413
11/4/202423
11/5/202433
11/6/202400
11/7/202400
11/8/202412
11/9/202422
23/15/202419
23/16/202429
23/17/202439
23/18/202449
23/19/202459
23/20/202469
23/21/202479
23/22/202489
23/23/202499
35/10/202415
35/11/202425
35/12/202435
35/13/202445
35/14/202455
35/15/202400
35/16/202400
35/17/202400
35/18/202417
35/19/202427
35/20/202437
35/21/202447
35/22/202457
35/23/202467
35/24/202477
35/25/202400
35/26/202400

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
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.

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26
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.

--
Paige Miller
Amelia6
Obsidian | Level 7

It works perfectly. Thank you so much!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 805 views
  • 0 likes
  • 2 in conversation