Hello All, I want to flag ID's with consecutive flags as 1 and those without consecutive flags as 0. For example ID 1 is consecutive because it starts from cycle 1 to cycle 4 so I create a flag for ID 1 as 1. However for ID's with no consecutive cycle numbers like ID 2 (its starts from cycle 2 to cycle 5, missing cycle 1) and ID 3 also starts from cycle 8 missing cycles 1-7, I flags these ID's as 0. Is there a way to go about this. Thank you.
Data have;
input ID $1. Cycle;
Datalines;
1 1
1 1
1 2
1 3
1 4
2 3
2 4
2 5
3 8
3 8
;
run;
Run a DO loop:
data want;
flag = 1;
cy = -1;
do until (last.id);
set have;
by id;
if cycle gt cy + 1 then flag = 0;
cy = cycle;
end;
keep id flag;
run;
(untested, posted from my tablet)
If you want to "remerge" the flag to the original dataset, add a second DO loop that reads the same group and OUTPUTs.
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.