BookmarkSubscribeRSS Feed
Denali
Quartz | Level 8

Hi,

 

I have a dataset contains 551 variables (ID, var1, var2, var3.....var550). I would like to rename all the var1-var550 with a "_chg" at the end (var1_chg, var2_chg,......var550_chg). What's the best way to rename all 550 variables?

 

Thank you!

4 REPLIES 4
novinosrin
Tourmaline | Level 20

Hi @Denali 

 

/*Your sample HAVE*/
data have;
 array col(550);
run;
/*Rename*/
data _null_;
 if 0 then set have;
 array c col:;
 call execute('proc datasets lib=work; modify have;rename');
do over c;
  call execute(cats(vname(c),'=',vname(c),'_chg  '));
end;
 call execute(';quit;');
run;

Of course you can query dictionary columns /sashelp vcolumns and get the same . I just fancied something of the above kind 

mkeintz
PROC Star

This is a common request.  The strategy is to (1) get the metadata (in this case all the variable names, each followed by and equal sign and a corresponding new variable name) into a macro variable,  Then include this macrovar in a rename statement in a PROC DATASETS, as in:

 

 

data have;
  array v {500} var1-var500;
  do _n_=1 to dim(v);  v{_n_}=_n_; end;
  output;
  stop;
run;

proc sql noprint;
  select 
    cats(name,'=',name,'_chg')
   into :rename_list separated by ' '
  from dictionary.columns where libname='WORK' and memname='HAVE';
quit;


proc datasets library=work nolist;
  modify have;
  rename &rename_list ;
  run;
quit;

 

 

There are a number of metadata resource in various members of the DICTIONARY container, which is accessible only via a proc sql. This particular members here is COLUMNS.

 

regards,

Mark

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

This should get you started. In the PROC SQL put the library name and dataset name, in upper case, where I show LIB and DATASET. This extracts the names of the variables if they actually start with VAR. The data set is used to create proc datasets rename statement. Make sure to use the same library and dataset names that you did in the Proc SQL step.

 

proc sql;
   create table work.rename as
   select name, cats(name,'_chg') as newname
   from dictionary.columns
   where libname='LIB' and memname='DATASET'
   and upcase(name) like 'VAR%'
   ;
quit;

data _null_;
   set work.rename end=LastName;
   if _n_ = 1 then do;
      /* LIB and DATASET below are the library and data set name used above*/
      Call execute ("Proc datasets library=LIB nodetails nolist;");
      Call execute ("modify DATASET;");
      Call execute ("rename ")  ;
   end;
   Call execute(catx(' ',name,' = ',newname)) ;
   if LastName then do;
      Call execute (";") ;
      Call execute ("quit;");
   end;
run;
Reeza
Super User

If you're willing to change the name slightly this can be a one liner. 

 

rename var1-var550 = var_chg_1-var_chg_550;

@Denali wrote:

Hi,

 

I have a dataset contains 551 variables (ID, var1, var2, var3.....var550). I would like to rename all the var1-var550 with a "_chg" at the end (var1_chg, var2_chg,......var550_chg). What's the best way to rename all 550 variables?

 

Thank you!


 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1421 views
  • 8 likes
  • 5 in conversation