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

I have a combined dataset containing diagnosis codes from 2 different sources. Each source has multiple diagnosis code fields.
For brevity, I only added a 7 of the 55 desired diagnosis fields. Please see attached sample data.

 

I want to keep the original variables & create a ‘Combined_Dxcode’ variable with 7 fields.

so need:

ComorbidityCode1-ComorbidityCode4 renamed to 'Combined_Dxcode1 - Combined_Dxcode4'
DgnsCode1 - DgnsCode3 renamed to 'Combined_Dxcode5 - Combined_Dxcode7'

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You don’t need macro code. You can use it but why not just use two rename statements like I included at the bottom of my example? 

 

View solution in original post

3 REPLIES 3
Reeza
Super User

@eap wrote:

I have a combined dataset containing diagnosis codes from 2 different sources. Each source has multiple diagnosis code fields.
For brevity, I only added a 7 of the 55 desired diagnosis fields. Please see attached sample data.

 

I want to keep the original variables & create a ‘Combined_Dxcode’ variable with 7 fields.

so need:

ComorbidityCode1-ComorbidityCode4 renamed to 'Combined_Dxcode1 - Combined_Dxcode4'
DgnsCode1 - DgnsCode3 renamed to 'Combined_Dxcode5 - Combined_Dxcode7'

 

Thank you!


Are you creating one or two variables?

Are you renaming the old variables or are those the names of the new variables. SAS names are typically less than 32 characters. 

 

Either way, here's examples of how to combine and rename and hopefully you can figure it out from here:

 

data have;
    input ComorbidityCode1 $ ComorbidityCode2 $ ComorbidityCode3 $  ComorbidityCode4 $  DgnsCode1 $ DgnsCode2 $ DgnsCode3 $ @@;
    datalines;
M1611 V1016
T84XA S4587
M1712 A1515
M1630
;
run;

data want;
set have;
DX1_DX3 = catx(", ", of DgnsCode1-DgnsCode3);
CMD1_CMD4 = catx(", ", of ComorbidityCode1-ComorbidityCode4);
All = catx(", " , of DgnsCode1-DgnsCode3, of ComorbidityCode1-ComorbidityCode4 );

rename All = newVariableName;

/*rename a series at once*/
rename DgnsCode1-DgnsCode3=DX1-DX3;
run;

FYI - your data step code had a mistake in it, extra $. 

 


@eap wrote:

I have a combined dataset containing diagnosis codes from 2 different sources. Each source has multiple diagnosis code fields.
For brevity, I only added a 7 of the 55 desired diagnosis fields. Please see attached sample data.

 

I want to keep the original variables & create a ‘Combined_Dxcode’ variable with 7 fields.

so need:

ComorbidityCode1-ComorbidityCode4 renamed to 'Combined_Dxcode1 - Combined_Dxcode4'
DgnsCode1 - DgnsCode3 renamed to 'Combined_Dxcode5 - Combined_Dxcode7'

 

Thank you!


 

 

 

eap
Obsidian | Level 7 eap
Obsidian | Level 7

I'm trying to create 7 consecutive new variables Combined_Dxcode&i where each new field will contain a single, identical diagnosis code from:

  • the 4 variables ComorbidityCode&i (ComorbidityCode1, ComorbidityCode2, ComorbidityCode3, ComorbidityCode4)
  • the 3 variables DgnsCode&i (DgnsCode1, DgnsCode2, DgnsCode3, DgnsCode4)

 

For example,

  • Comorbidity1 becomes Combined_Dxcode1
  • Comorbidity4 becomes Combined_Dxcode4 BUT
  • DgnsCode1 becomes Combined_Dxcode5 ...
  • DgnsCode3 becomes Combined_Dxcode7

 

I was thinking something like below but this logic doesn't work correctly for DgnsCode1, DgnsCode2, DgnsCode3

      %do i=1 %to 4;
      Combined_Dxcode&i = ComorbidityCode&i;
      %end;

 

Reeza, thanks for letting me know about the extra '$'!

Reeza
Super User

You don’t need macro code. You can use it but why not just use two rename statements like I included at the bottom of my example? 

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 708 views
  • 1 like
  • 2 in conversation