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?
thanks.
/* 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;
data want;
set have;
rename trt1-trt9=col1-col9;
run;
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?
@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.
/* 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;
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.
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.