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!
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.
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.
Thanks a lot! I tested it and it works fine.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: