I want to flag the treatment if certain conditions are met but I don't want to flag it again for the same patient. The condition here is, if the patient is taking drug A. B, C or D for the very first time and the ENROL_FLAG = "Y". If the first condition doesn't get satisfied then the second condition is that ENROL_FLAG = "Y" and the DAY_DIFF > 180. Also if the patient is receiving two or more treatment on the same day where he is meeting this criteria all the observations should get flagged. The code I wrote is, data WANT; set HAVE; by ID DATE; if first.ID and ENROL_FLAG = "Y" then TRT_FLAG= "Y"; else if ENROL_FLAG="Y" and DAY_DIFF > 180 then TRT_FLAG="Y"; run; This is how my data looks like: ID DRUG DATE VISIT ENROL_FLAG DAY_DIFF 1 A 29APR2018 1 Y . 1 D 29APR2018 1 Y . 1 A 21MAY2019 2 Y 22 1 D 21MAY2019 2 Y 22 2 B 27JUN2015 1 Y . 2 B 26JUN2016 2 Y 365 3 C 03MAR2015 1 N . 3 B 03MAR2015 1 N . 3 C 12SEP2015 2 Y 193 3 B 12SEP2015 2 Y 193 The result I am getting is: ID DRUG DATE VISIT ENROL_FLAG DAY_DIFF TRT_FLAG 1 A 29APR2018 1 Y . Y 1 D 29APR2018 1 Y . 1 A 21MAY2019 2 Y 22 1 D 21MAY2019 2 Y 22 2 B 27JUN2015 1 Y . Y 2 B 26JUN2016 2 Y 365 Y 3 C 03MAR2015 1 N . 3 B 03MAR2015 1 N . 3 C 12SEP2015 2 Y 193 Y 3 B 12SEP2015 2 Y 193 The result I want: ID DRUG DATE VISIT ENROL_FLAG DAY_DIFF TRT_FLAG 1 A 29APR2018 1 Y . Y 1 D 29APR2018 1 Y . Y 1 A 21MAY2019 2 Y 22 1 D 21MAY2019 2 Y 22 2 B 27JUN2015 1 Y . Y 2 B 26JUN2016 2 Y 365 3 C 03MAR2015 1 N . 3 B 03MAR2015 1 N . 3 C 12SEP2015 2 Y 193 Y 3 B 12SEP2015 2 Y 193 Y
... View more