Hi nandini, this is based on the comparing the currents records from previous records by ae terms and dates, so there are two flags one for dates and one of ae terms. As per the output you have given where you mentioned that we need to compare the first record with all other records, does not meet the output you displayed. This is because for subject 102 the first ae record is rash and next none of the records have rash. however in the output you have flagged constipation which is not the first record. as per my understanding you are trying to compare the current record with previous record, then constipation will be flagged. Please try the below code and check an let me know if you getting the desired output. let me know if it does not meet you expectation. data aes; informat ae_start date9. ae_stop date9.; input @1 subject $3. @5 ae_start date9. @15 ae_stop date9. @25 adverse_event $15.; format ae_start ae_stop date9.; datalines; 101 01JAN2004 02JAN2004 Headache 101 02JAN2004 03FEB2004 Back Pain 102 03NOV2003 10DEC2003 Rash 102 10NOV2003 10JAN2004 Abdominal Pain 102 04APR2004 06APR2004 Constipation 102 05jul2004 07jul2004 Back pain 102 05APR2004 07APR2004 Constipation 102 04APR2004 04APR2004 Headache ; run; proc sort data=aes; by subject ae_start ae_stop; run; data want; set aes; by subject ae_start ae_stop; old_aest=lag(ae_start); old_aeend=lag(ae_stop); first_ae=lag(adverse_event); if first.subject then do; old_aest=.; old_aeend=.; first_ae=''; end; if not first.subject then do; if (old_aest<=ae_start <= old_aeend) or (old_aest<=ae_stop <= old_aeend) then flag='Yes'; else flag='No'; if first_ae=adverse_event then ae_flag='Yes'; else ae_flag='No'; end; format old_aest old_aeend date9.; run; proc print; run; Thanks, jagadish
... View more