06-20-2016 04:19 PM
Hello! I was wondering if anyone knows why y=20.25 shows up in my output data although I specified "while (y<20)." I am not sure what I am doing wrong here.
do i= 1 to 5 by 0.5 while (y<20);
y=i**2; /*values are 1, 2.25, 4, 6.25,...,16*/
proc print data=do_while_data;
Here is my output data:
06-20-2016 04:41 PM
You're not doing anything wrong, it's where the WHILE condition is evaluated
The expression is evaluated at the top of the loop before the statements in the DO loop are executed. If the expression is true, the DO loop iterates. If the expression is false the first time it is evaluated, the DO loop does not iterate even once.
In your case, when it enters the loop its 16, at the end it's 20.25 so it doesn't iterate again.
06-20-2016 05:17 PM
When SAS hits the DO loop, y is 0. So the WHILE condition is false at that point, and the loop always iterates at least once.
After the loop iterates once, it is possible that the WHILE condition becomes false and the loop ends.