BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SASAID
Calcite | Level 5
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.
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@SASAID

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;

View solution in original post

4 REPLIES 4
TomKari
Onyx | Level 15

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;
Reeza
Super User

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.

 

Patrick
Opal | Level 21

@SASAID

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;
SASAID
Calcite | Level 5

Perfect! Thank you very much

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to connect to databases in SAS Viya

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.

Discussion stats
  • 4 replies
  • 2331 views
  • 0 likes
  • 4 in conversation