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

hello,

 

I have a dataset 'final' that contains following variables (info from DICTIONARY.COLUMNS) I would like to re3name all variable that start with 'trt' to 'COL' , number counting from 1 to number of variables with name started with 'trt'. Is it a way to do this rename without listing one by one, or counting how many variables that need to be updated manually?

stataq_0-1698247176015.png

thanks.

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
/* UNTESTED CODE, in fact this is DOUBLY UNTESTED CODE */

proc contents data=have noprint out=_contents_;
run;
data _contents1_;
    set _contents_(where=(upcase(name)=:'TRT'));
    name1=cats('col',_n_);
run;
proc sql noprint;
    select cats(name,'=',name1) into :renames separated by ' '
    from _contents1_;
run;
proc datasets library=work nolist;
     modify have;
     rename &renames;
run; quit;
--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26
data want;
    set have;
    rename trt1-trt9=col1-col9;
run;
--
Paige Miller
stataq
Quartz | Level 8

the tricky part is the var 'trt' is not from 1-9, they are 1,3,4,5,6,7,99, so the new name will be COL+1,2,3,4,5,6,7. Is it possible to auto update 'variable with name started with trt' to name 'COL'+the variable order in the dataset?

ballardw
Super User

@stataq wrote:

the tricky part is the var 'trt' is not from 1-9, they are 1,3,4,5,6,7,99, so the new name will be COL+1,2,3,4,5,6,7. Is it possible to auto update 'variable with name started with trt' to name 'COL'+the variable order in the dataset?


SAS provides tools to deal with basic sequentially numbered lists, not intermittent lists like yours.

 

The amount of code to do such "automagically", if that is what you are requesting, will take way more time to write, test, debug (and make flexible) than it will to write a Rename statement. Though I would suggest doing it in Proc Datasets.

 

proc datasets library=work;
   modify final;
   rename 
      trt1=col1
      trt3=col2
      trt4=col3
      trt5=col4
      trt6=col5
      trt7=col6
      trt99=col7
   ;
run;
quit;

If you perceive a need to do this frequently then I would ask why the variables have the "incorrect" names to begin with, or at least do not have the trt1-trt7 when created.

 

I went through a similar "rename everything" exercise once. That after 5 hours and a number of trials we had reduced the number of "exceptions to the general rule" that had been simply stated as "rename everything that starts with "xxxxx_x" to "yyyyy_y", basically provided a "one word to another word", to only 25 or so cases that still had to be manually addressed because the starting rule missed other compound strings that matched.

 

PaigeMiller
Diamond | Level 26
/* UNTESTED CODE, in fact this is DOUBLY UNTESTED CODE */

proc contents data=have noprint out=_contents_;
run;
data _contents1_;
    set _contents_(where=(upcase(name)=:'TRT'));
    name1=cats('col',_n_);
run;
proc sql noprint;
    select cats(name,'=',name1) into :renames separated by ' '
    from _contents1_;
run;
proc datasets library=work nolist;
     modify have;
     rename &renames;
run; quit;
--
Paige Miller

sas-innovate-white.png

🚨 Early Bird Rate Extended!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Lock in the best rate now before the price increases on April 1.

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
  • 4 replies
  • 1317 views
  • 1 like
  • 3 in conversation