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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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

4 REPLIES 4
Rick_SAS
SAS Super FREQ

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";
dougc
SAS Employee

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";

 

 

 

Rick_SAS
SAS Super FREQ

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. 

Notifications
Calcite | Level 5

That was the trick I was looking for. Nice!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 4 replies
  • 2227 views
  • 0 likes
  • 3 in conversation