🔒 This topic is solved and locked.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 05-10-2019 08:27 AM
(723 views)
Hello there,
I would appreciate if you could help me to calculate the number of consecutives occurences.
Here is an example
Have
ID | Group |
1 | 0 |
2 | 1 |
3 | 0 |
4 | 1 |
5 | 1 |
6 | 0 |
7 | 0 |
Want
ID | Group | Count |
1 | 0 | 0 |
2 | 1 | 1 |
3 | 0 | 0 |
4 | 1 | 2 |
5 | 1 | 2 |
6 | 0 | 0 |
7 | 0 | 0 |
Thanks in advance!
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Here's an approach:
data want;
counter = 0;
do until (last.group);
set have;
by group notsorted;
counter + 1;
end;
do until (last.group);
set have;
by group notsorted;
if group = 0 then count = 0;
else count = counter;
output;
end;
drop counter;
run;
It's untested code, so you will have to try it to see if it needs any adjustments.
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Here's an approach:
data want;
counter = 0;
do until (last.group);
set have;
by group notsorted;
counter + 1;
end;
do until (last.group);
set have;
by group notsorted;
if group = 0 then count = 0;
else count = counter;
output;
end;
drop counter;
run;
It's untested code, so you will have to try it to see if it needs any adjustments.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks a lot! I tested it and it works fine.