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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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