BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
stataq
Quartz | Level 8

Hello, 

 

I have multiple datasets with different col numbers.

 

I would like to rename each data set as:

1. 1st col as "label"

2. 2nd col to last col="Col1" to "Coln";

 

where n= colum position-1;

 

How can I rename data set without doing rename 1 by 1?

 

Any suggestion?

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

Similar approach to data _null_'s.  User PROC CONTENTs to get the metadata on variable number and name, then use PROC SQL to build the macro variable with list of renames:

 

1    proc contents data=sashelp.class out=varinfo (keep=name varnum) ;
2    run ;

NOTE: The data set WORK.VARINFO has 5 observations and 2 variables.

3
4    proc sql noprint ;
5      select cats(name,'=',ifc(varnum=1,"label",cats('Col',varnum-1)))
6      into :renamelist separated by " "
7      from varinfo
8      order by varnum
9     ;
NOTE: The query as specified involves ordering by an item that doesn't appear in its SELECT clause.
10   quit ;

11
12   %put &renamelist;
Name=label Sex=Col1 Age=Col2 Height=Col3 Weight=Col4
The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.

View solution in original post

4 REPLIES 4
data_null__
Jade | Level 19

Create a list of variables names(PROC TRANSPOSE) in VARNUM order and create new names using your rule.  Note the OBS=0 in the data= options.

 

data heart;
   set sashelp.heart;
   run;
proc transpose data=heart(obs=0) out=vnames;
   var _all_;
   run;

data rename;
   set vnames;
   length newname $32;
   select(_n_);
      when(1)     newname = 'Label';
      otherwise   newname = catx('_','Col',_n_-1);
      end;
   run;

proc print;
   run;
proc sql noprint;
   select catx('=',_name_,newname) into :renlist separated by ' ' 
      from rename;
   quit;
%put NOTE: &=renlist;

proc datasets nolist;
   modify heart;
      rename &renlist;
      run;
   contents data=heart varnum;
   quit;
Quentin
Super User

Similar approach to data _null_'s.  User PROC CONTENTs to get the metadata on variable number and name, then use PROC SQL to build the macro variable with list of renames:

 

1    proc contents data=sashelp.class out=varinfo (keep=name varnum) ;
2    run ;

NOTE: The data set WORK.VARINFO has 5 observations and 2 variables.

3
4    proc sql noprint ;
5      select cats(name,'=',ifc(varnum=1,"label",cats('Col',varnum-1)))
6      into :renamelist separated by " "
7      from varinfo
8      order by varnum
9     ;
NOTE: The query as specified involves ordering by an item that doesn't appear in its SELECT clause.
10   quit ;

11
12   %put &renamelist;
Name=label Sex=Col1 Age=Col2 Height=Col3 Weight=Col4
The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.
Astounding
PROC Star
On the surface, this seems like a horrible idea. You would make your data incredibly difficult to program with, and create the situation where col3 means something different depending on which data set you are working with.

Perhaps you could describe what happens next,, after the names have been changed. We might be able to suggest a less destructive path to take to your goal.
PaigeMiller
Diamond | Level 26

@Astounding wrote:
On the surface, this seems like a horrible idea. You would make your data incredibly difficult to program with, and create the situation where col3 means something different depending on which data set you are working with.

Perhaps you could describe what happens next,, after the names have been changed. We might be able to suggest a less destructive path to take to your goal.

Thank you for saying this @Astounding . I agree completely, HORRIBLE IDEA. Just because you CAN do this, it doesn't mean you SHOULD do this. It seems like extra work for no benefit at all.


@stataq what is the reason you want to do this work? What is the benefit?

--
Paige Miller

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 Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 833 views
  • 11 likes
  • 5 in conversation