Not sure about a use case for this other than "academic" but here you go:
data have;
set sashelp.class;
run;
%let nvarsToChange=3;
proc sql noprint;
select
cats(name,'=',substrn(cats('renamed_',name),1,32)) into :_rename_list separated by ' '
from dictionary.columns
where
libname='WORK'
and memname='HAVE'
and varnum<=&nvarsToChange
;
quit;
%put %nrbquote(&_rename_list);
proc datasets lib=work nolist nowarn;
modify have;
rename &_rename_list;
run;
contents data=have order=varnum;
run;
quit;
Can't completely answer this one without knowing a little more about what you're trying to do, but here's how to get the column names in order.
Tom
proc sql noprint;
create table x as select name from dictionary.columns where
libname = "SASHELP" and memname = "SHOES" and varnum <= 5
order by varnum;
run;
Yes it’s possible by reading the names into macro variables and using those instead.
@SASAID wrote:
Hello,
Is it possible to choose the first 5 columns of a table and to rename it without knowing the variable names ? Thanks a lot for answering my question.
Not sure about a use case for this other than "academic" but here you go:
data have;
set sashelp.class;
run;
%let nvarsToChange=3;
proc sql noprint;
select
cats(name,'=',substrn(cats('renamed_',name),1,32)) into :_rename_list separated by ' '
from dictionary.columns
where
libname='WORK'
and memname='HAVE'
and varnum<=&nvarsToChange
;
quit;
%put %nrbquote(&_rename_list);
proc datasets lib=work nolist nowarn;
modify have;
rename &_rename_list;
run;
contents data=have order=varnum;
run;
quit;
Perfect! Thank you very much
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.