SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
AbuChowdhury
Fluorite | Level 6

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?

6 REPLIES 6
Reeza
Super User

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

Rename ar1=CA35543 ar2=CH5783 etc;

AbuChowdhury
Fluorite | Level 6

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?

AbuChowdhury
Fluorite | Level 6

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;

Reeza
Super User

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

AbuChowdhury
Fluorite | Level 6

I have names of the variables in this way:


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

Reeza
Super User

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.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1262 views
  • 0 likes
  • 2 in conversation