Statistical programming, matrix languages, and more

Break/Continue statements in proc iml loops

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Break/Continue statements in proc iml loops

What is the Proc IML equivalent of break/continue statments found in other languages, such as MATLAB and Python?


Accepted Solutions
Solution
‎03-02-2017 02:14 PM
SAS Super FREQ
Posts: 3,618

Re: Break/Continue statements in proc iml loops

Well, kinda. @dougc's code works if you are in a module but in open code it is invalid. The doc says "The GOTO statement must be inside a module or DO group. These statements must be able to resolve the referenced label within the current unit of statements."  What that means is that you can't jump out of a do loop unless the GOTO statement and label are both within some larger nested DO/END block that you submit, like this:

 

do;
   /* BREAK: exit loop immediately */
   count = 0;
   do i = 1 to 1000;
      roll = sample(die, 1);   /* roll die 1 time */
      if roll = 6 then goto BREAK;
      count = count + 1;
      /* other statements */
   end;
   BREAK:
   print count "rolls before rolling a 6"; 
end;

Personally, I do not recommend jumping outside of a loop. I think the DO UNTIL syntax is clearer and easier to read. 

View solution in original post


All Replies
SAS Super FREQ
Posts: 3,618

Re: Break/Continue statements in proc iml loops

In the SAS DATA step, you can use the LEAVE and CONTINUE statements.

 

DO loops in SAS/IML do not support those statements. However, you can use a DO UNTIL loop to break out of a loop when a certain condition is met.  You can use a GOTO/LABEL statement to jump to the end of a loop and continue with the next iteration when a certain condition is met.  Here is an example of each construct:

 

/* simulate the roll of a six-sided die */
proc iml;
call randseed(54321);
die = 1:6;

/* BREAK: Exit loop when a condition is true */
done = 0;  /* set flag to FALSE */
do i = 1 to 1000 until(done);
   roll = sample(die, 1);   /* roll die 1 time */
   if roll = 6 then done = 1;
end;
print i " rolls until roll = " roll;


/* CONTINUE: Skip the body and go to the next iteration 
             when a condition is true */
count = 0;
do i = 1 to 1000;
   roll = sample(die, 1);   /* roll die 1 time */
   if roll = 6 then goto CONTINUE;
   count = count + 1;
CONTINUE:
end;
print count " rolls were not 6";
SAS Employee
Posts: 9

Re: Break/Continue statements in proc iml loops

You can also use a modification of Rick's CONTINUE example to break out of a loop immediately.  This would only be important if you had other code in the loop that you didn't want to execute

 

/* BREAK: exit loop immediately */
count = 0;
do i = 1 to 1000;
   roll = sample(die, 1);   /* roll die 1 time */
   if roll = 6 then goto BREAK;
   count = count + 1;
   /* other statements */
end;
BREAK:
print count "rolls before rolling a 6";

 

 

 

Solution
‎03-02-2017 02:14 PM
SAS Super FREQ
Posts: 3,618

Re: Break/Continue statements in proc iml loops

Well, kinda. @dougc's code works if you are in a module but in open code it is invalid. The doc says "The GOTO statement must be inside a module or DO group. These statements must be able to resolve the referenced label within the current unit of statements."  What that means is that you can't jump out of a do loop unless the GOTO statement and label are both within some larger nested DO/END block that you submit, like this:

 

do;
   /* BREAK: exit loop immediately */
   count = 0;
   do i = 1 to 1000;
      roll = sample(die, 1);   /* roll die 1 time */
      if roll = 6 then goto BREAK;
      count = count + 1;
      /* other statements */
   end;
   BREAK:
   print count "rolls before rolling a 6"; 
end;

Personally, I do not recommend jumping outside of a loop. I think the DO UNTIL syntax is clearer and easier to read. 

New Contributor
Posts: 2

Re: Break/Continue statements in proc iml loops

That was the trick I was looking for. Nice!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 329 views
  • 0 likes
  • 3 in conversation