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

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
Rick_SAS
SAS Super FREQ

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

4 REPLIES 4
Rick_SAS
SAS Super FREQ

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;

jazhang
Fluorite | Level 6
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!
Rick_SAS
SAS Super FREQ

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

jazhang
Fluorite | Level 6

Thanks Rick, this is very helpful!

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
  • 2914 views
  • 1 like
  • 2 in conversation