Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename variables

Status
Not open for further replies.

bumed

Technical User
Aug 23, 2007
18
US
May problem is that I need to change the variable names in an excel sheet so that all the names have _W1 on them. I found a macro that can do this put I'm unsure of how to use it. I used proc contents to pull the names and symput to make then into a macro variable but I cant seem to get it to change the names in data Q. I know Im missing a step. Thanks for the help.

PROC IMPORT OUT= WORK.Q
DATAFILE= "C:\Documents and Settings\boraste\My Documents\SB
_LS_111407.xls"
DBMS=EXCEL REPLACE;
SHEET="SB_Q2";
GETNAMES=YES;
MIXED=NO;
SCANTEXT=YES;
USEDATE=YES;
SCANTIME=YES;
RUN;
proc contents data = Q out=Qcon noprint;

data Qcon1;
set Qcon (keep = Label);
OPTIONS MPRINT;
data _null_;
set Qcon1;
call symput("vlabel",Label);

%macro rename;
%local varlist i var;
%let name= &vlabel;
%let i=0;
%do %while(%scan(&varlist,&i+1,%str( )) ne %str( ));
%let i = %eval(&i+1);
%let var = %scan(&varlist,&i,%str( ));
&var = W1&var
%end;
%mend rename;

%rename
run;
proc print data = Q;
 
You are going to have to do this in a macro and use the RENAME statement.

Here is how:

Code:
proc contents
   data = Q
   out  = Qcon noprint;
run;

%macro ChangeNames;
data _null_;
  set Qcon end=last;
  call symput('varnm'||trim(left(put(_n_,8.))), trim(name) );
  if last then
    call symput('lim',trim(left(put(_n_,8.))) );
run;

data Q;
  set Q_newnames;
  RENAME 
  %do i=1 %to &lim;
    &&varnm&i = &&varnm&i.._w1
  %end;
  ;
run;
%mend ChangeNames;
%ChangeNames;

Try this and see if it works for you. I still am not sure why you would want to change variable names enmass, but to each their own way.


Klaz
 
Wicked cool. The reason I need to this is that I'm working with a longitudinal research database and there is the same variables for many years or waves (W1 W2 ...). So in need to change the names to put it into SPSS.
Thanx
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top