sas - Flagging the first instance in an episode without transposing -
i'm working in sas , i'm trying create column disease_flag flags first row given disease code occurs in array. in case disease code care 'a36'. ideally without first transposing.
so data looks this:
episode_id diagcode1 diagcode2 diagcode3 121 a36 b11 121 a36 b11 b12 121 b12 b05 b06 122 b12 122 a36 b12 b13 122 b12 b01 123 b12 b13 b11 123 b12 a36 123 b13 b12
i want add additional column called disease_flag flags first instance of a36 in array of columns diagcode1--diagcode3.
thus final output this:
episode_id diagcode1 diagcode2 diagcode3 disease_flag 121 a36 b11 1 121 a36 b11 b12 0 121 b12 b05 b06 0 122 b12 0 122 a36 b12 b13 1 122 b12 b01 0 123 b12 b13 b11 0 123 b12 a36 1 123 b13 b12 0
you can using retained helper variable resets 0 each id , stays set 1 first time a36 found:
data have; input episode_id diagcode1 $ diagcode2 $ diagcode3 $; infile cards missover; cards; 121 a36 b11 121 a36 b11 b12 121 b12 b05 b06 122 b12 122 a36 b12 b13 122 b12 b01 123 b12 b13 b11 123 b12 a36 123 b13 b12 ; run; data want; set have; episode_id; retain t_flag; if first.episode_id t_flag = 0; disease_flag = (diagcode1 = 'a36' or diagcode2 = 'a36' or diagcode3 = 'a36') , t_flag = 0; if disease_flag t_flag = 1; drop t_flag; run;
Comments
Post a Comment