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!
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?
@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!
I'm trying to create 7 consecutive new variables Combined_Dxcode&i where each new field will contain a single, identical diagnosis code from:
For example,
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 '$'!
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?
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!
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.
Ready to level-up your skills? Choose your own adventure.