BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bkq32
Quartz | Level 8

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @bkq32  Please see if this helps

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;
 set have;
 array p patient_id_25--patient_id_2029;
 do over p;
  if input(scan(vname(p),-1,'_'),best.)= patient_id then do;
   origvalue=p;
   leave;
  end;
 end;
 drop patient_id_25--patient_id_2029;
run;

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

Hi @bkq32  Please see if this helps

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;
 set have;
 array p patient_id_25--patient_id_2029;
 do over p;
  if input(scan(vname(p),-1,'_'),best.)= patient_id then do;
   origvalue=p;
   leave;
  end;
 end;
 drop patient_id_25--patient_id_2029;
run;
bkq32
Quartz | Level 8

@novinosrin This works great - thank you very much!

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 719 views
  • 0 likes
  • 2 in conversation