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

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
BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: 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
BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: 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-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 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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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