I have ID's with multiple flags in cycles however some id's flags are not consecutive. For example ID A has two flags in cycle 1 but none in cycle 2,3,4 but then has another flag in cycle 5. I want to keep only the flags in cycle 1 and delete flags in cycle 5 since there is a break in cycle at 2. Another example is ID B has flags in cycles 1, 2,3 but not in cycle 4 however has flags in cycles 5 and 6. Since there is a break in cycle at 4, I want to keep the consecutive flags in cycles 1,2,3 and delete cycles 5 and 6. Can someone help me with this. Sample data is below
DATA chk;
INPUT ID $ Flag Cycle ;
CARDS;
A 1 1
A 1 1
A 1 5
A 1 5
B 1 1
B 1 2
B 1 3
B 1 5
B 1 6
C 1 1
C 1 1
C 1 2
C 1 4
C 1 5
;
RUN;
This is the output I want
1 | A | 1 | 1 |
2 | A | 1 | 1 |
3 | B | 1 | 1 |
4 | B | 1 | 2 |
5 | B | 1 | 3 |
6 | C | 1 | 1 |
7 | C | 1 | 1 |
8 | C | 1 | 2 |
Like this?
data HAVE;
input ID $ FLAG CYCLE ;
cards;
A 1 1
A 1 1
A 1 5
A 1 5
B 1 1
B 1 2
B 1 3
B 1 5
B 1 6
C 1 1
C 1 1
C 1 2
C 1 4
C 1 5
run;
data WANT;
set HAVE;
by ID;
DEL_FLG= ifn( first.ID , .
, ifn( CYCLE>lag(CYCLE)+1 , 1
, DEL_FLG ));
if DEL_FLG then delete;
retain DEL_FLG;
run;
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.