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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

View solution in original post

15 REPLIES 15
Haikuo
Onyx | Level 15

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

MikeZdeb
Rhodochrosite | Level 12

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

Linlin
Lapis Lazuli | Level 10

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;

MikeZdeb
Rhodochrosite | Level 12

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

Linlin
Lapis Lazuli | Level 10

Thank you Mike! It is my first time to see coding rename the way you coded.

Haikuo
Onyx | Level 15

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

shellp55
Quartz | Level 8

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.

Linlin
Lapis Lazuli | Level 10

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;

shellp55
Quartz | Level 8

Thank you so much, Linlin and HaiKuo!!!  That worked perfectly!!

shellp55
Quartz | Level 8

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.

ballardw
Super User

Post the code you ran. Likely you have the dx1 in a position that SAS is interpreting as a "from" name.

shellp55
Quartz | Level 8

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.

Tom
Super User Tom
Super User

Your code is doing the opposite of what you want.  Syntax for RENAME is

rename OLDNAME=NEWNAME

shellp55
Quartz | Level 8

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 15 replies
  • 5971 views
  • 5 likes
  • 6 in conversation