Hello,
I have multiple datasets with different col numbers.
I would like to rename each data set as:
1. 1st col as "label"
2. 2nd col to last col="Col1" to "Coln";
where n= colum position-1;
How can I rename data set without doing rename 1 by 1?
Any suggestion?
Similar approach to data _null_'s. User PROC CONTENTs to get the metadata on variable number and name, then use PROC SQL to build the macro variable with list of renames:
1 proc contents data=sashelp.class out=varinfo (keep=name varnum) ; 2 run ; NOTE: The data set WORK.VARINFO has 5 observations and 2 variables. 3 4 proc sql noprint ; 5 select cats(name,'=',ifc(varnum=1,"label",cats('Col',varnum-1))) 6 into :renamelist separated by " " 7 from varinfo 8 order by varnum 9 ; NOTE: The query as specified involves ordering by an item that doesn't appear in its SELECT clause. 10 quit ; 11 12 %put &renamelist; Name=label Sex=Col1 Age=Col2 Height=Col3 Weight=Col4
Create a list of variables names(PROC TRANSPOSE) in VARNUM order and create new names using your rule. Note the OBS=0 in the data= options.
data heart;
set sashelp.heart;
run;
proc transpose data=heart(obs=0) out=vnames;
var _all_;
run;
data rename;
set vnames;
length newname $32;
select(_n_);
when(1) newname = 'Label';
otherwise newname = catx('_','Col',_n_-1);
end;
run;
proc print;
run;
proc sql noprint;
select catx('=',_name_,newname) into :renlist separated by ' '
from rename;
quit;
%put NOTE: &=renlist;
proc datasets nolist;
modify heart;
rename &renlist;
run;
contents data=heart varnum;
quit;
Similar approach to data _null_'s. User PROC CONTENTs to get the metadata on variable number and name, then use PROC SQL to build the macro variable with list of renames:
1 proc contents data=sashelp.class out=varinfo (keep=name varnum) ; 2 run ; NOTE: The data set WORK.VARINFO has 5 observations and 2 variables. 3 4 proc sql noprint ; 5 select cats(name,'=',ifc(varnum=1,"label",cats('Col',varnum-1))) 6 into :renamelist separated by " " 7 from varinfo 8 order by varnum 9 ; NOTE: The query as specified involves ordering by an item that doesn't appear in its SELECT clause. 10 quit ; 11 12 %put &renamelist; Name=label Sex=Col1 Age=Col2 Height=Col3 Weight=Col4
@Astounding wrote:
On the surface, this seems like a horrible idea. You would make your data incredibly difficult to program with, and create the situation where col3 means something different depending on which data set you are working with.
Perhaps you could describe what happens next,, after the names have been changed. We might be able to suggest a less destructive path to take to your goal.
Thank you for saying this @Astounding . I agree completely, HORRIBLE IDEA. Just because you CAN do this, it doesn't mean you SHOULD do this. It seems like extra work for no benefit at all.
@stataq what is the reason you want to do this work? What is the benefit?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.