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

I want to do:

%DO i = 1 %to 100; but I want to skip some numbers, e.g. let said 9, and 50;

My code is:

%DO i = 1 %TO 8;

%FUNC;

%END;

%DO i = 10 %TO 49;

%FUNC;

%END;

%DO i = 51 %TO 100;

%FUNC;

%END;

Is there any way to make it more efficient? Thanks!!

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

Hi,

If you don't mind a %GOTO, you could do something like:

%macro loop(dummy);
  %local i;
  %do i=1 %to 100;
    %if &i=9 or &i=50 %then %goto skipped;

    %*Main code here;
    %put i=&i;

    %skipped:
  %end;
%mend loop;

%loop()


There is a popular ballotware suggestion for enhancements to macro looping, to give it more of the features of data step do loops:

https://communities.sas.com/ideas/1084

HTH,

--Q.

View solution in original post

4 REPLIES 4
Quentin
Super User

Hi,

If you don't mind a %GOTO, you could do something like:

%macro loop(dummy);
  %local i;
  %do i=1 %to 100;
    %if &i=9 or &i=50 %then %goto skipped;

    %*Main code here;
    %put i=&i;

    %skipped:
  %end;
%mend loop;

%loop()


There is a popular ballotware suggestion for enhancements to macro looping, to give it more of the features of data step do loops:

https://communities.sas.com/ideas/1084

HTH,

--Q.

jwillis
Quartz | Level 8

%macro testit2;

%DO i = 1 %to 100;

   %if ((&i. ne %eval(9)) and (&i. ne %eval(50))) %then %do;

          %put "i = &i.";

   %end;

%END;

%mend testit2;

%testit2;

pawan
Obsidian | Level 7

Hi Ken, I think you can give a try as below...

%DO i = 1 %TO 8, 10 %TO 100;

or

%DO i = 1 %TO 8, 10 %TO 49, 50 %TO 100;

Above code worked for me. All the Best!!

Quentin
Super User

pawan wrote:

Hi Ken, I think you can give a try as below...

%DO i = 1 %TO 8, 10 %TO 100;

or

%DO i = 1 %TO 8, 10 %TO 49, 50 %TO 100;

Above code worked for me. All the Best!!

Unfortunately, that sort of list works for data step DO loops, but not macro %DO loops:

16         %macro loop(dummy);
17         %do i = 1 %TO 8, 10 %TO 20;
18         %end;
19         %mend;
NOTE: The macro LOOP completed compilation without errors.
      9 instructions 184 bytes.
20         %loop()
ERROR: A character operand was found in the %EVAL function or %IF condition where a 
       numeric operand is required. The condition was: 8, 10 %TO 20 
ERROR: The %TO value of the %DO I loop is invalid.
ERROR: The macro LOOP will stop executing.


hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 5436 views
  • 3 likes
  • 4 in conversation