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,
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.
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.
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.
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.
@PaigeMiller wrote:
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.
@mkeintz wrote:
@PaigeMiller wrote:
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.
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;
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.
@PaigeMiller wrote:
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;
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!
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.