BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dustychair
Pyrite | Level 9

Hi,

i have 42 variables with long names in my data. I want to change their names to V1, V2 etc. Instead of writing each of their names is there a quick way to rename them?

 

Thanks,

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

You could hardcode a RENAME statement in a PROC DATASETS, as per this example for a dataset named WORK.CLASS:

 

data class;
  set sashelp.class;
run;
proc datasets library=work nolist;
  modify class;
    rename name=v1 sex=v2 age=v3  height=v4 weight=v5;
  run;
quit;

You can also dynamically discover the old variable names and generate the corresponding RENAME statement, which could then the %INCLUDEd in the PROC DATASETS:

 

data class;
  set sashelp.class;
run;
filename tmp temp;
data _null_;
  if 0 then set class;
  length _vname $20;
  file tmp;
  put 'rename ' @;
  do i=1 by 1 until (upcase(_vname)='_VNAME');
   call vnext(_vname);
   if upcase(_vname)='_VNAME' then leave;
   put _vname +(-1) '=V' i  @;
  end;
  put ';';
run;
proc datasets library=work  nolist;
  modify class;
    %include tmp / source2;
  run;
quit;

 

The "IF 0 then SET statement" reads no data, but it doesn't have to.  It does force the SAS compiler to generate the program data vector (i.e. the list of variables), That can be sequentially examined via the CALL VNEXT statement, which allows for generation of expressions like  "name=v1", "sex=v2", etc.   Write those expressions to a temporary text file, and %INCLUDE the result having all the renames in the subsequent PROC DATASETS.

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

--------------------------

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26

Instead of making meaningless name like V1-V42 that no one will understand ...

 

Most if not all SAS procedures (exception: SQL) allow variable lists. So for example in PROC MEANS, you indicate the first and last variable name which are sequential in the data set separated by a double-dash, to include all the variables between the first variable name and the last variable name (inclusive), as follows: 

 

proc means data=have;
    var verylongvariablenamenumber1--superduperlongvariablename;
run;

And of course you can use any other options and statements in PROC MEANS as you would like.

--
Paige Miller
dustychair
Pyrite | Level 9
Thank you for your response @PaigeMiller. My variables names are not sequential. I will use them for another purpose in a different program. I am just organizing them in SAS. so I don't need to run proc means.

Thanks
PaigeMiller
Diamond | Level 26

When I say sequential, I don't mean variable names such as v1 v2 v3 v4, I mean the position in the data set is sequential, left to right. If the columns are adjacent then you can do this.

 

What do you mean you are just organizing them in SAS? For what purpose? What happens next after you organize??


I did mention PROC MEANS is an example. The method works in almost any PROC (except SQL) and it works in DATA steps.

--
Paige Miller
mkeintz
PROC Star

You could hardcode a RENAME statement in a PROC DATASETS, as per this example for a dataset named WORK.CLASS:

 

data class;
  set sashelp.class;
run;
proc datasets library=work nolist;
  modify class;
    rename name=v1 sex=v2 age=v3  height=v4 weight=v5;
  run;
quit;

You can also dynamically discover the old variable names and generate the corresponding RENAME statement, which could then the %INCLUDEd in the PROC DATASETS:

 

data class;
  set sashelp.class;
run;
filename tmp temp;
data _null_;
  if 0 then set class;
  length _vname $20;
  file tmp;
  put 'rename ' @;
  do i=1 by 1 until (upcase(_vname)='_VNAME');
   call vnext(_vname);
   if upcase(_vname)='_VNAME' then leave;
   put _vname +(-1) '=V' i  @;
  end;
  put ';';
run;
proc datasets library=work  nolist;
  modify class;
    %include tmp / source2;
  run;
quit;

 

The "IF 0 then SET statement" reads no data, but it doesn't have to.  It does force the SAS compiler to generate the program data vector (i.e. the list of variables), That can be sequentially examined via the CALL VNEXT statement, which allows for generation of expressions like  "name=v1", "sex=v2", etc.   Write those expressions to a temporary text file, and %INCLUDE the result having all the renames in the subsequent PROC DATASETS.

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

--------------------------
PaigeMiller
Diamond | Level 26

@mkeintz 

 

When not use name--weight and skip the renaming? Much less typing.

--
Paige Miller
mkeintz
PROC Star

@PaigeMiller wrote:

@mkeintz 

 

When not use name--weight and skip the renaming? Much less typing.


To be honest, I just accepted the OP's request for renaming, without any further thought.

 

But as I think about it a little more, using name--weight requires knowledge of variable names in advance.

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

--------------------------
PaigeMiller
Diamond | Level 26

@mkeintz wrote:

@PaigeMiller wrote:

@mkeintz 

 

When not use name--weight and skip the renaming? Much less typing.


To be honest, I just accepted the OP's request for renaming, without any further thought.

 

But as I think about it a little more, using name--weight requires knowledge of variable names in advance.


Yes it does require the names of two variables in advance, and I assume the user already knows that since he/she was going to type out all 42 variable names and then decided to ask the question about renaming to V1 V2 V3 ... .

 

I find this another example of the XY problem, where the user @dustychair is extremely fixated on a specific task that is unnecessary (and in this case, I believe harmful, to convert meaningful variable names in to meaningless V1 V2 V3 ...), rather than asking us about the broader problem in which case perhaps making meaningless variable names or renaming isn't really needed and much easier solutions are available.

--
Paige Miller
dustychair
Pyrite | Level 9
Thank you!
Tom
Super User Tom
Super User

Easy to do, but WHY?

So assuming your dataset is named HAVE and it is in the WORK library then this code will generate a series of old=new name pairs into a macro variable which can then be used in a RENAME statement or RENAME= dataset option.

proc sql noprint;
select catx('=',nliteral(name),cats('v',varnum))
  into :rename separated by ' '
  from dictionary.columns
  where libname='WORK' and memname='HAVE'
    and lowcase(name) ne cats('v',varnum)
;
quit;
proc dataset nolist lib=WORK;
  modify HAVE ;
  rename &rename;
  run;
quit;
PaigeMiller
Diamond | Level 26

@Tom 


Why is PROC DATASETS needed here? Just extract the variable names using PROC SQL into a macro variable without renaming, and use the macro variable.

--
Paige Miller
Tom
Super User Tom
Super User

@PaigeMiller wrote:

@Tom 


Why is PROC DATASETS needed here? Just extract the variable names using PROC SQL into a macro variable without renaming, and use the macro variable.


That is a question for the OP.  It is hard to perform a rename without actually performing a rename.  You could also use the OLD=NEW pairs in a RENAME= dataset option.  For example IF the reason for the renaming is the need to deliver a dataset using those new names you might just do something like one of these data steps.

data want;
  set have(rename=(&rename));
run;

data want;
  set have ;
  rename &rename;
run;

data want(rename=(&rename));
  set have;
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 11 replies
  • 1552 views
  • 0 likes
  • 4 in conversation