proc sort data=have; by pnr atc_code type date; run; data need; set have; length change $ 10.; by pnr atc_code; /*create new variable change and keep constant if it is topical for first observation*/ /*compare topical values with next values if it changes to other non bio or bio then switched*/ if first.pnr and atc_code in ( 'D05AX52','D05AX02') then change=""; else if not first.pnr and atc_code in('D05AX52','D05AX02','L01XC02','L04AA24','L04AB01','L04AB02','L04AB04','L04AB05','L04AB06','L04AC03','L04AC07','A11CC03','D05BB02','L01BA01','L01BB03' ,'L01XX05' ,'L04AA21' ,'L04AD01' ,'L04AX03') then change='switched'; /*compare non bio values with next values if it changes to other non bio or bio then switched*/ if first.pnr and atc_code in ('L01XC02','L04AA24','L04AB01','L04AB02','L04AB04','L04AB05','L04AB06','L04AC03','L04AC07') then change=""; else if not first.pnr and atc_code not in( 'D05AX52','D05AX02') and atc_code in('L01XC02','L04AA24','L04AB01','L04AB02','L04AB04','L04AB05','L04AB06','L04AC03','L04AC07','A11CC03','D05BB02','L01BA01','L01BB03' ,'L01XX05' ,'L04AA21' ,'L04AD01' ,'L04AX03') then change='switched'; /*compare bio with next values if it changes to other bio then switched*/ if first.pnr and atc_code in('A11CC03','D05BB02','L01BA01','L01BB03' ,'L01XX05' ,'L04AA21' ,'L04AD01' ,'L04AX03') then change=''; else if not first.pnr and atc_code not in( 'D05AX52','D05AX02','L01XC02','L04AA24','L04AB01','L04AB02','L04AB04','L04AB05','L04AB06','L04AC03','L04AC07') and atc_code in('A11CC03','D05BB02','L01BA01','L01BB03' ,'L01XX05' ,'L04AA21' ,'L04AD01' ,'L04AX03') then change='switched'; if atc_code=lag(atc_code) then change=""; run;
... View more