Help using Base SAS procedures

Rename Variable List

Accepted Solution Solved
Reply
Regular Contributor
Posts: 152
Accepted Solution

Rename Variable List

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.


Accepted Solutions
Solution
‎09-28-2012 01:39 PM
Respected Advisor
Posts: 3,124

Re: Rename Variable List

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


All Replies
Solution
‎09-28-2012 01:39 PM
Respected Advisor
Posts: 3,124

Re: Rename Variable List

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

Valued Guide
Posts: 765

Re: Rename Variable List

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

Super Contributor
Posts: 1,636

Re: Rename Variable List

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;

Valued Guide
Posts: 765

Re: Rename Variable List

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

Super Contributor
Posts: 1,636

Re: Rename Variable List

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

Respected Advisor
Posts: 3,124

Re: Rename Variable List

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

Regular Contributor
Posts: 152

Re: Rename Variable List

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.

Super Contributor
Posts: 1,636

Re: Rename Variable List

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;

Regular Contributor
Posts: 152

Re: Rename Variable List

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

Regular Contributor
Posts: 152

Re: Rename Variable List

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.

Super User
Posts: 10,500

Re: Rename Variable List

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

Regular Contributor
Posts: 152

Re: Rename Variable List

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.

Super User
Super User
Posts: 6,499

Re: Rename Variable List

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

rename OLDNAME=NEWNAME

Regular Contributor
Posts: 152

Re: Rename Variable List

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.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 15 replies
  • 511 views
  • 4 likes
  • 6 in conversation