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.

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

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.

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
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.


BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

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