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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.