Fluorite | Level 6

## How to break from the do loop in SAS IML

Hello,

I would like to translate a matlab code into SAS IML but have some issues on the translating break function in a do loop.

Basically what I would like to do is like below:

proc iml;
x=-3;
do i = 1 to 10;
if x>0 then do;
y[i] = sqrt(x);
print y;
goto break;
else do;
y[i]=x#2;
print y;
end;
print x;
end;

break:

The code above does not work properly, I want to make it break from the do loop when x is positive so that only y is printed. If x is negative, then both x and y will be printed. Please if there are other functions in IML can realize this function. Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
SAS Super FREQ

## Re: How to break from the do loop in SAS IML

Did you read the article that I linked to?  It shows two ways to do this:

1. protect the remainder of the loop with

if ( ^done ) then do;

/* stuff you don't want to execute */

end;

2. Use the GOTO statement to jump to the end of the loop.

``````proc iml;
x=-3;
y = j(1, 10, .);
done = 0;
do i = 1 to 10 while( ^done );
if x>0 then do;
y[i] = sqrt(x);
print y;
done = 1;  /* set exit criterion */
goto breakout;
end;
else do;
y[i]=x#2;
print y;
end;
x = x + 1;  /* change x? */
breakout:
end;
print x;``````

Please study the links and code so that you can learn how to transfer your MATLAB knowledge into SAS/IML. You might also want to read "Ten tips for learning the SAS/IML language."

4 REPLIES 4
SAS Super FREQ

## Re: How to break from the do loop in SAS IML

The SAS/IML supports the DO WHILE and DO UNTIL statements, which are logically equivalent to the BREAK statement,

Your program does not change the value of x within the loop, so I'm not sure what logic you are trying to accomplish, but the general idea is

``````proc iml;
x=-3;
y = j(1, 10, .);
done = 0;
do i = 1 to 10 while( ^done );
if x>0 then do;
y[i] = sqrt(x);
print y;
done = 1;  /* set exit criterion */
end;
else do;
y[i]=x#2;
print y;
end;
x = x + 1;  /* change x? */
end;
print x;

``````
Fluorite | Level 6

## Re: How to break from the do loop in SAS IML

Thanks for the quick reply, Rick! This is only a simplified code of original Matlab code but does the same function. But I would like to immediately jump out of the loop after y is printed when x is positive. But I think your code will still run to the end of the current loop. Thanks!
SAS Super FREQ

## Re: How to break from the do loop in SAS IML

Did you read the article that I linked to?  It shows two ways to do this:

1. protect the remainder of the loop with

if ( ^done ) then do;

/* stuff you don't want to execute */

end;

2. Use the GOTO statement to jump to the end of the loop.

``````proc iml;
x=-3;
y = j(1, 10, .);
done = 0;
do i = 1 to 10 while( ^done );
if x>0 then do;
y[i] = sqrt(x);
print y;
done = 1;  /* set exit criterion */
goto breakout;
end;
else do;
y[i]=x#2;
print y;
end;
x = x + 1;  /* change x? */
breakout:
end;
print x;``````

Please study the links and code so that you can learn how to transfer your MATLAB knowledge into SAS/IML. You might also want to read "Ten tips for learning the SAS/IML language."

Fluorite | Level 6

## Re: How to break from the do loop in SAS IML

Thanks Rick, this is very helpful!

From The DO Loop