BookmarkSubscribeRSS Feed
Paakay
Calcite | Level 5

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; 

 

1 REPLY 1
Kurt_Bremser
Super User

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.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

Register now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 805 views
  • 0 likes
  • 2 in conversation