Hi guys, I thought Square is the same thing as n**2 because they are just the opposite side of an equation.
but I really cannot figure our why it shows different result when I run the following two codes;
data square;
do n = 1 to 1000 while (n**2 le 100);
Square = n**2;
output;
end;
put n= ; The result is N=11
run;
data square;
do n = 1 to 1000 while (Square le 100);
Square = n**2;
output;
end;
put n= ; The result is N=12 :smileyshocked:
run;
n Square
1 1
2 4
... ...
10 100
11 121
12 144
But since the OP was Putting the value of n After the execution of the loop it has n=12. The do set the value of N then evaluates Square. So after leaving the loop n is one larger.
Has nothing to do with the ** but the behavior of DO WHILE and evaluation of Missing values.
DO while evaluates at the start of the loop. In the first case when n**2 is used n has a value (1) and is evaluated. With the second case Square does not have any value until AFTER the first execution of the comparison (Square le 100) and since Missing is Always less than any given value it evaluates as true. Do a proc print after each of the data steps and you'll see that behavior and the second version being one step "later" for values of Square.
Values N=11 or N=12 are the loop terminators when condition was evaluated as false. In your dataset square you will have the right numbers.
Hi chouchou,
Using this, I am getting 11 values in dataset square.
data square;
do n = 1 to 1000 while (Square le 100);
Square = n**2;
output;
end;
run;
proc print data=square;
run;
But since the OP was Putting the value of n After the execution of the loop it has n=12. The do set the value of N then evaluates Square. So after leaving the loop n is one larger.
@ ballardw: Agreed, dataset square will have 11 values and n will have maximum value of 11 while square will be 121 against this. After the termination of while loop now n has value of 12 which is being used in put statement and log shown in log as 12. If we use following, it will give us wrong numbers
data square;
do n = 1 to 1000 while (Square le 100);
Square = n**2;
end;
run;
proc print data=square;
run;
Because you are referencing the square of different values of N in the WHILE () condition.
In the first WHILE(n**2 <= 100) you are referencing the N that you just incremented and in the second WHILE(square<=100) you are referencing the square of the N from the previous iteration of the loop.
The value AFTER the loop is larger than the value in the last iteration of the loop because you are using a WHILE condition instead of an UNTIL condition.
Try :
do n = 1 to 1000 until (n**2 > 100);
or
do n = 1 to 1000 until (square > 100);
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.