Statistical programming, matrix languages, and more

How to break from the do loop in SAS IML

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

How to break from the do loop in SAS IML

[ Edited ]

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!


Accepted Solutions
Solution
‎07-11-2017 09:37 PM
SAS Super FREQ
Posts: 3,753

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

View solution in original post


All Replies
SAS Super FREQ
Posts: 3,753

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;

Occasional Contributor
Posts: 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!
Solution
‎07-11-2017 09:37 PM
SAS Super FREQ
Posts: 3,753

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

Occasional Contributor
Posts: 6

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

Thanks Rick, this is very helpful!

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 214 views
  • 1 like
  • 2 in conversation