Help with Rename all variables into a1 a2 a3....

Accepted Solution Solved
Reply
Super Contributor
Posts: 420
Accepted Solution

Help with Rename all variables into a1 a2 a3....

Hi Everyone,

I have a dataset with an ID variable and many variables with random names.

Is there any way to turn variables (non ID) name into say a1 a2 a3....a40 a50 ==> data file look like ID;a1;a2...

It will help me better when I run say proc means; var a1-a50...

Thank you.

HHC


Accepted Solutions
Solution
‎12-03-2014 05:35 PM
Super User
Posts: 19,855

Re: Help with Rename all variables into a1 a2 a3....

The SASHELP.CLASS has 3 numeric variables side by side in the dataset.

If your variables are not side by side you'll need some other way of listing them.

Try the following:

proc means data=sashelp.class;

var age--weight;

run;

View solution in original post


All Replies
Super User
Posts: 19,855

Re: Help with Rename all variables into a1 a2 a3....

You can dynamically build your rename statement using the sashelp.vcolumn table:

If ID isn't your first variable you may need to use a second step to get the numbering proper. The comparisons in the libname/memname/name are case sensitive.

proc sql;

select catx("=", name, catt('a',varnum-1)) into :rename_list separated by " "

from sashelp.vcolumn

where upper(libname)='SASHELP' and upper(memname)='CLASS'

AND upper(name) ne 'NAME';

quit;

data class;

set sashelp.class;

run;

proc datasets library=work nolist nodetails;

modify class;

rename &rename_list;

run; quit;

PROC Star
Posts: 7,487

Re: Help with Rename all variables into a1 a2 a3....

Rather than rename your variables why not just use a different type of list? namely using the -- operator

i.e., var firstvarablename--lastvariablename

where you replace firstvarablename with your first variable name, and lastvariablename with your files last bariable of interest.

Super Contributor
Posts: 420

Re: Help with Rename all variables into a1 a2 a3....

Hi Arthur,

Could you be more specific?

I never work with these operation before.

HHC

Super User
Posts: 5,516

Re: Help with Rename all variables into a1 a2 a3....

How would it be helpful to see variable names of a1-a50 instead of the actual variable names?

If you have a few variables that shouldn't be processed by PROC MEANS, it might be easier to code:

proc means data=have (drop=id zipcode);

var _numeric_;

run;

But I still can't picture why you would want to change the names of the variables.

Super Contributor
Posts: 420

Re: Help with Rename all variables into a1 a2 a3....

Posted in reply to Astounding

Thanks everyone,

I cannot test it right now. I'll do it tomorrow.

The only reason makes me changing name to a1-a30 is that

when I do proc means, I have to list all name in Var.

As the name is long and follow no rule and the name is a mix of character and number (say t1vx4 gd5_5xd) I thought "it could be better to make it a1-a30".

Thank you.

HHC

Super Contributor
Posts: 420

Re: Help with Rename all variables into a1 a2 a3....

That works perfectly. Thank you so much!

HHC

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

data class;

set sashelp.class;

run;

data class; set class;

newvar=1;

id=_n_;run;

proc means data=class;

var age--id;

run;

proc means data=class (DROP=age);

var _numeric_;

run;

*see content of file;

proc contents data=sashelp.class nodetails varnum;

run;

Solution
‎12-03-2014 05:35 PM
Super User
Posts: 19,855

Re: Help with Rename all variables into a1 a2 a3....

The SASHELP.CLASS has 3 numeric variables side by side in the dataset.

If your variables are not side by side you'll need some other way of listing them.

Try the following:

proc means data=sashelp.class;

var age--weight;

run;

PROC Star
Posts: 7,487

Re: Help with Rename all variables into a1 a2 a3....

Expanding on what said, if you run:

proc contents data=sashelp.class nodetails varnum;

run;

You will see the order of the variables.  Thus, age--weight, will produce age, height and weight since age is the 3rd variable, height the 4th variable and weight the 5th variable.

If you had indicated name--weight, you would get name, sex, age, height and weight.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 440 views
  • 6 likes
  • 4 in conversation