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!
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."
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;
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."
Thanks Rick, this is very helpful!
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.