BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
chouchou
Calcite | Level 5

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  Smiley Happy

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

6 REPLIES 6
ballardw
Super User

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.

stat_sas
Ammonite | Level 13

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.

stat_sas
Ammonite | Level 13

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;

ballardw
Super User

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.

stat_sas
Ammonite | Level 13

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

Tom
Super User Tom
Super User

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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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