Hi, I am looking for an efficient way to create two flags based on ID level patterns of observation types and I am struggling to find pertinent documentation. With this data the focus tends be on the most recent observation for an ID, but in some cases I need to look back when certain criteria are met. The first flag I would like to create is when there are two observations with type=99, but only when there are no observations with type 10 or 4 in between them. The second flag I would like to create is when value= 2, but only in cases where the following two observations have value =1 and then value= 0 respectively (no missing values may be in between). Ideally in this instance I would also pull the date from the most recent observation with value=0 to the observation with the value=2. With respect to flag1 - in the past I kept only type=99 observations and used lag to pull in values from the prior observation. However that approach will no longer work because if there are one or more observations in between that do have type 10 or 4 then that ID is disqualified. Flag2 is a new need so I haven't coded it yet. My thought was to use a double lag, but in the rare instance that there are multiple zeros, ideally I would pull in the date from the most recent 0. Any suggestions are much appreciated! Here is simplified input data for have and want datasets- data have; input ID $ date date9. type$ type2; format date date9.; datalines; A 06JUN2025 11 2 A 13JUN2025 1 1 A 13JUN2025 10 2 A 15JUN2025 1 1 A 23JUN2025 99 0 A 27JUN2025 99 0 B 13JUN2025 1 . B 14JUN2025 99 . B 16JUN2025 11 2 B 23JUN2025 1 1 B 26JUN2025 99 0 C 15JUN2025 99 . C 23JUN2025 10 . C 27JUN2025 99 . D 01JUN2025 10 2 D 5JUN2025 99 1 D 20JUN2025 3 0 D 29JUN2025 99 0 ; run; data want; input ID $ date date9. type$ flag1 type2 flag2 date2 date9.; format date date2 date9.; datalines; A 06JUN2025 11 . 2 . A 13JUN2025 1 . 1 . A 13JUN2025 10 . 2 1 27JUN2025 A 15JUN2025 1 . 1 . A 23JUN2025 99 1 0 . A 27JUN2025 99 1 0 . B 13JUN2025 1 . . . B 14JUN2025 99 0 . . B 16JUN2025 11 . 2 1 26JUN2025 B 23JUN2025 1 . 1 . B 26JUN2025 99 0 0 . C 15JUN2025 99 0 . . C 23JUN2025 10 . . . C 27JUN2025 99 0 . . D 01JUN2025 10 . 2 1 29JUN2025 D 5JUN2025 99 1 1 . D 20JUN2025 3 . 0. . D 29JUN2025 99 1 0 . ; run;
... View more