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

Hi, instead of writing all the below out for 100 variables I would like to simplify it with a macro do loop. However, I am unsure of how I would do this as I need not only the value of 'i' but the value of 'i'+1 in each iteration of the do loop to make it work. How would I resolve this issue - see in the macro I am trying to write below, I have left question marks where I am unsure how to write a variable that I essentially want to resolve as x(&i.+1). is there any way of actually writing this do loop so I can simplify the x1=x2 x2=x3 etc.?

 

data want;

set have;

rename x1=x2 x2=x3 x3=x4 x4=x5.....x99=x100;

run;

 

%macro abc;

data want;

set have;

rename %do i=1 %to 99; x&i.=x????? %end;;

run;

mend;

%abc;

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Major error: missing % for %mend statement. If you have actually run that code the SAS session is likely unstable.

 

%macro abc;
data want;
   set have;
   rename 
    %do i=1 %to 99; 
       x&i.=x%eval(&i+1)
    %end;
   ; 
run;
%mend;

Please post code in a code box opened on the forum with either the </> or "running man" icon.

Note the indentation to tell where the macro do loop starts and ends a bit easier than all on one line and that the semicolon that ends the Rename is also more visible to that purpose.

 

If the sole purpose of code is to rename variables then you should do it that with Proc Datasets. A data step like this has to process every observation and for large data sets code be quite time consuming. Proc datasets will do this in place.

Proc datasets library=work;
   modify have;
   rename x1=x2
          x2=x3
          <repeat>
   ;
run;
quit;

View solution in original post

2 REPLIES 2
ballardw
Super User

Major error: missing % for %mend statement. If you have actually run that code the SAS session is likely unstable.

 

%macro abc;
data want;
   set have;
   rename 
    %do i=1 %to 99; 
       x&i.=x%eval(&i+1)
    %end;
   ; 
run;
%mend;

Please post code in a code box opened on the forum with either the </> or "running man" icon.

Note the indentation to tell where the macro do loop starts and ends a bit easier than all on one line and that the semicolon that ends the Rename is also more visible to that purpose.

 

If the sole purpose of code is to rename variables then you should do it that with Proc Datasets. A data step like this has to process every observation and for large data sets code be quite time consuming. Proc datasets will do this in place.

Proc datasets library=work;
   modify have;
   rename x1=x2
          x2=x3
          <repeat>
   ;
run;
quit;
EC27556
Quartz | Level 8
Fab, thank you, works great 🙂

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 420 views
  • 0 likes
  • 2 in conversation