Hello
I have a dataset where the data is in wide format. I have a range called dx1-dx25 which I'd like to rename diagcde1-diagcde25. I have 3 other ranges like this that will need to be changed.
I searched the site and was able to successfully change singular variable names using "modify" and "rename" but I'm not sure how to do a range. Some of the examples on the sites indicated that I would need to transpose this range first, is that true?
Any and all assistance greatly appreciated.
Macro is handy there.
%macro renm;
%do i=1 %to 25;
dx&i=diagcde&i
%end;
%mend;
data want;
set have;
rename %renm;
run;
Haikuo
Macro is handy there.
%macro renm;
%do i=1 %to 25;
dx&i=diagcde&i
%end;
%mend;
data want;
set have;
rename %renm;
run;
Haikuo
hi ... if you have V9.1 or greater ...
data x;
retain dx1-dx3 250;
run;
data x;
set x;
rename dx1-dx3=diagcde1-diagcde3;
run;
diagcde1 diagcde2 diagcde3
250 250 250
I came up a more complicated one:smileysilly:. you have to change the RED parts to suit your data.
/* create sample dataset */
data have;
array _dx dx1-dx25;
do over _dx;
_dx=2;
end;
/**********************/
%let lib=work; /* the library of your dataset */
%let dsn=have; /* your dataset name */
proc sql noprint;
select catx('=',name,cats('diagcde',compress(name,,'kd'))) into :names separated by ' '
from sashelp.vcolumn
where libname="%upcase(&lib)" and memname="%upcase(&dsn)" and upcase(substr(name,1,2))='DX';
proc datasets lib=&lib;
modify &dsn;
rename &names;
quit;
proc contents data=&lib..&dsn;run;
hi ...PROC DATASETS is the better route (though I'm not sure if it's worth all the extra code with a small data set),
I think it's odd that this type of statement ...
rename dx1-dx3=diagcde1-diagcde3;
is NOT allowed in PROC DATASETS, only in a data step
ps a paper ... "Renaming in Batches"
http://support.sas.com/resources/papers/proceedings09/075-2009.pdf
Thank you Mike! It is my first time to see coding rename the way you coded.
I asked myself why I always fall with the most strenuous ways of doing things? Of course, proc datasets is the way to go, for just operating on the descriptor. Thanks, Mike.
Haikuo
Wow, thanks for the multiple responses in such a short period of time!!
Mike, each dx will have its own code or be blank so do I need to include the 250? Also, how does this work with the singular edits i.e. this is the code thus far without the diagnoses:
datasets lib=hims;
modify hims.mydb;
rename gender=sex admitcat=admit entrycd=entry dischdate=sepdate totallos=los;
run;
Thanks.
Hi,
Mike's code is not allowed in proc datasets. try adding HaiKuo's code:
%macro renm;
%do i=1 %to 25;
dx&i=diagcde&i
%end;
%mend;
proc datasets lib=hims;
modify mydb;
rename gender=sex admitcat=admit entrycd=entry dischdate=sepdate totallos=los %renm;
run;
Thank you so much, Linlin and HaiKuo!!! That worked perfectly!!
Hi I know this post is old but I'm using it now and for some reason the code is excluding the first entry. So I use the renaming code that changes dxcode1 to dx1 but I get the error message "the variable dx1 in the drop, keep or rename list has never been referenced". I know that there is for sure a diagcde1 so why would this be happening? Note that it happens with all the ranges I'm trying to change and not just diagnoses. Any help greatly appreciated.
Post the code you ran. Likely you have the dx1 in a position that SAS is interpreting as a "from" name.
Thanks for the quick response. Below is the code: %macro renm1; %do i=1 %to 20; int_code_&i=pxcde&i; %end; %mend; So pxcde1 to pxcde20 is the current name and I wish to change it to int_code_1 to int_code_20; Thanks.
Your code is doing the opposite of what you want. Syntax for RENAME is
rename OLDNAME=NEWNAME
Tom, that caused issues so that now it's indicating that ALL the other ones are uninitialized i.e. variable int_code_2 is uninitialized all the way down to 20. Prior to your response, I changed the do loop to %do i=0 %to 20; int_code_&i=pxcde&i; and that worked for all 20. But I would like to know why I'm getting this error. Thanks.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.