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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

4 REPLIES 4
art297
Opal | Level 21

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

 

art297
Opal | Level 21

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

 

Reeza
Super User

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;




mikiduta
Fluorite | Level 6

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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