DATA Step, Macro, Functions and more

rename variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

rename variables

Hi,

 

I have a dataset with many variables, named as it follows: 

street_density, landuse_mix, street_connectivity, deprivation

 

I would like to automatically rename them as

x1= street_density, x2=landuse_mix, x3=street_connectivity, x4=deprivation.

 

I am looking for an easy way to do this for many variables simultaneously.

 

Thanks!


Accepted Solutions
Solution
‎02-17-2017 04:10 PM
PROC Star
Posts: 7,366

Re: rename variables

The reason I asked you to post the proc standard code that you ran is because you can copy the variable list it contains into a data step, then use the proc sql method I mentioned earlier this afternoon. Here is an example using sashelp.class:

 

PROC STANDARD DATA= sashelp.class MEAN=0 STD=1 OUT=zscoreswk;
  VAR age height weight;
run;

data need;
  input;
  _n_=1;
  do while (scan(_infile_,_n_) ne '');
    var=scan(_infile_,_n_);
    output;
    _n_+1;
  end;
  cards;
age height weight
;

proc sql noprint;
  select catt(var,'=z_',var)
    into :vars separated by ' '
      from need
  ;
quit;

data zscoreswk;
  set zscoreswk;
  rename &vars.;
run;

HTH,

Art, CEO, AnalystFinder.com

 

View solution in original post


All Replies
PROC Star
Posts: 7,366

Re: rename variables

I thought this was what you were asking in the first place. Please post the actual proc standard code that you ran.

 

Art, CEO, AnalystFinder.com

 

Solution
‎02-17-2017 04:10 PM
PROC Star
Posts: 7,366

Re: rename variables

The reason I asked you to post the proc standard code that you ran is because you can copy the variable list it contains into a data step, then use the proc sql method I mentioned earlier this afternoon. Here is an example using sashelp.class:

 

PROC STANDARD DATA= sashelp.class MEAN=0 STD=1 OUT=zscoreswk;
  VAR age height weight;
run;

data need;
  input;
  _n_=1;
  do while (scan(_infile_,_n_) ne '');
    var=scan(_infile_,_n_);
    output;
    _n_+1;
  end;
  cards;
age height weight
;

proc sql noprint;
  select catt(var,'=z_',var)
    into :vars separated by ' '
      from need
  ;
quit;

data zscoreswk;
  set zscoreswk;
  rename &vars.;
run;

HTH,

Art, CEO, AnalystFinder.com

 

Super User
Posts: 17,963

Re: rename variables

Here's an example of renaming, using the sashelp library. I've broken it out into more steps that ideally necessary, but it helps you to understand what's happening. You can combine steps as you're comfortable with the program. 

 

data class;
set sashelp.class;
run;

data names;
set sashelp.vcolumn;
where libname='SASHELP' and memname='CLASS';

new_name = catt("X", _n_);

keep new_name name;
run;

proc sql noprint;
select catx("=", name, new_name) into :rename_list separated by " "
from names;
quit;

proc datasets lib=work nodetails nolist;
modify class;
rename &rename_list;
run;quit;




Occasional Contributor
Posts: 11

Re: rename variables

Thank you both, Reeza and Art! Your responses are both very helpful! Have a nice afternoon!

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 169 views
  • 3 likes
  • 3 in conversation