This is the dataset I have and the one I want to create. I'd like to create a new variable ORIGVALUE that takes the value of the PATIENT_ID_: variables where the variable name suffix equals the PATIENT_ID.
data have;
length patient_id 8. variable $200.;
input patient_id variable $ patient_id_25 patient_id_2020 patient_id_2029;
cards;
25 MomWtGain 21 25 8
25 Weight 4054 2835 2920
2020 CigsPerDay 0 20 0
2029 MomWtGain 21 25 8
;
run;
data want;
length patient_id 8. variable $200.;
input patient_id variable $ origvalue $;
cards;
25 MomWtGain 21
25 Weight 4054
2020 CigsPerDay 20
2029 MomWtGain 8
;
run;
I can do it manually via the following, but have too many records and PATIENT_ID_: columns to do this. Is there a way to automate this?
data havea ( keep = patient_id variable patient_id_25
rename = ( patient_id_25 = origvalue ) )
haveb ( keep = patient_id variable patient_id_2020
rename = ( patient_id_2020 = origvalue ) )
havec ( keep = patient_id variable patient_id_2029
rename = ( patient_id_2029 = origvalue ) );
set have;
if patient_id = 25 then output havea;
if patient_id = 2020 then output haveb;
if patient_id = 2029 then output havec;
run;
data want;
set havea haveb havec;
run;