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 🙂

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 511 views
  • 0 likes
  • 2 in conversation