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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.