Solved
Contributor
Posts: 68

# do while

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

Accepted Solutions
Solution
‎10-15-2014 04:16 PM
Super User
Posts: 13,550

## Re: do while

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.

All Replies
Super User
Posts: 13,550

## Re: do while

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.

Trusted Advisor
Posts: 1,270

## Re: do while

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.

Trusted Advisor
Posts: 1,270

## Re: do while

Hi ,

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;

Solution
‎10-15-2014 04:16 PM
Super User
Posts: 13,550

## Re: do while

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.

Trusted Advisor
Posts: 1,270

## Re: do while

@ : 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;

Super User
Posts: 8,114

## Re: do while

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);

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 6 replies
• 344 views
• 6 likes
• 4 in conversation