turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- do while

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

10-15-2014 03:32 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

10-15-2014 04:16 PM

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

10-15-2014 03:50 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

10-15-2014 03:52 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

10-15-2014 04:11 PM

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;

Solution

10-15-2014
04:16 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

10-15-2014 04:16 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

10-15-2014 04:33 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

10-15-2014 08:10 PM

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