SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Choose the first 5 colums and rename with unknown variable names

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Choose the first 5 colums and rename with unknown variable names

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.

Accepted Solutions
Solution
3 weeks ago
Respected Advisor
Posts: 4,779

Re: Choose the first 5 colums and rename with unknown variable names

@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


All Replies
PROC Star
Posts: 1,333

Re: Choose the first 5 colums and rename with unknown variable names

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;
Super User
Posts: 23,980

Re: Choose the first 5 colums and rename with unknown variable names

[ Edited ]

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.

 

Solution
3 weeks ago
Respected Advisor
Posts: 4,779

Re: Choose the first 5 colums and rename with unknown variable names

@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;
New Contributor
Posts: 2

Re: Choose the first 5 colums and rename with unknown variable names

Perfect! Thank you very much

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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