Replacing the variable names

Reply
Regular Contributor
Posts: 183

Replacing the variable names

Hi Experts,

The sample of my original dataset looks as follows:

Date          ar1                 ar2                 ar3                 ar4                 ar5

......
.-0.0157268040.0176842945-0.016964348-0.000886750.0160770942
01/01/200400000
02/01/2004-0.00752637-0.0143337540.0208526907-0.0770407160.2230172601
05/01/2004-0.072398762-0.064743906-0.026989673-0.1276455860.0311527312
06/01/20040.00560408720.13730778070.01228908550.0957376211-0.025696325
07/01/20040.03103951720.036282484-0.008686090.0554872928-0.075945777
08/01/2004-0.0213514710.0808676035-0.0323164760.0035982172-0.042734186

However, I want to change the variable names. The new variables names will be

CA45245E1097CH0018666781US00208J1088US0025671050US0038301067

instead of ar1  ar2  ar3  ar4  ar5) and so on. Observations will remain same. Only variable names will have to be changed. How can I do that?

Super User
Posts: 19,170

Re: Replacing the variable names

Since there's no discernable pattern you'll need to manually use rename statements.

Rename ar1=CA35543 ar2=CH5783 etc;

Regular Contributor
Posts: 183

Re: Replacing the variable names

Can't we use macro for this? I see from the following link.

SAS Code Fragments: A few SAS macro programs for renaming variables dynamically

But in scenario number 1, it changed the names of only two variables. How can do that for all of my variables?

Regular Contributor
Posts: 183

Re: Replacing the variable names

I think that this code is helpful:

%macro rename1(oldvarlist, newvarlist);

  %let k=1;

  %let old = %scan(&oldvarlist, &k);

  %let new = %scan(&newvarlist, &k);

     %do %while(("&old" NE "") & ("&new" NE ""));

      rename &old = &new;

   %let k = %eval(&k + 1);

      %let old = %scan(&oldvarlist, &k);

      %let new = %scan(&newvarlist, &k);

  %end;

%mend;

data want ;

  set have;

  %rename1(ar1 ar2 ar3 ar4 ar5, CA45245E1097 CH0018666781 US00208J1088 US0025671050 US0038301067);

run;

But if I have ar1, ar2,  ......., ar200, then what can I do? I tried with the following but it's not working.

%let oldvarlist= ar1-ar5;

%let newvarlist= CA45245E1097 CH0018666781 US00208J1088 US0025671050 US0038301067;

data want ;

  set have;

  %rename1(&oldvarlist, &newvarlist);

run;

Super User
Posts: 19,170

Re: Replacing the variable names

I must have missed the part where you said you have 200 variables.

Do you have the name mappings somewhere?

See several options here:

sas - Create a sequence of new column names - Stack Overflow

Regular Contributor
Posts: 183

Re: Replacing the variable names

I have names of the variables in this way:


%let newvarlist= CA45245E1097 CH0018666781 US00208J1088 US0025671050 US0038301067 ..............;

Super User
Posts: 19,170

Re: Replacing the variable names

I'd recommend placing it into a dataset instead and using the answer from the link above. You wont need the first proc sql but the rest should be pretty much the same.

Ask a Question
Discussion stats
  • 6 replies
  • 252 views
  • 0 likes
  • 2 in conversation