I would like to assign ascending count for observations above certain value by id and start with the first observation of the id by 1 regardless of the value and continue the count if value is above 5 the output would be like:
id | days | event_count |
A | 1 | 1 |
A | 5 | 0 |
A | 2 | 0 |
A | 4 | 0 |
A | 7 | 2 |
A | 5 | 0 |
A | 8 | 3 |
A | 2 | 0 |
A | 1 | 0 |
A | 16 | 4 |
B | 1 | 1 |
B | 5 | 0 |
B | 2 | 0 |
B | 4 | 0 |
B | 7 | 2 |
B | 5 | 0 |
B | 8 | 3 |
B | 2 | 0 |
B | 1 | 0 |
B | 16 | 4 |
B | 4 | 0 |
B | 5 | 0 |
B | 3 | 0 |
B | 4 | 0 |
B | 7 | 5 |
B | 8 | 6 |
B | 8 | 7 |
Thank you.
data want;
set have;
by id;
if first.id then count=1;
if days>5 then count+1;
if days>5 or first.id then event_count=count;
else event_count=0;
drop count;
run;
data want;
set have;
by id;
if first.id then count=1;
if days>5 then count+1;
if days>5 or first.id then event_count=count;
else event_count=0;
drop count;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.