- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
/* 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data want;
set have;
rename trt1-trt9=col1-col9;
run;
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
/* 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