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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 1942 views
  • 0 likes
  • 4 in conversation