What is the Proc IML equivalent of break/continue statments found in other languages, such as MATLAB and Python?
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.
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";
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";
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.
That was the trick I was looking for. Nice!
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.