BookmarkSubscribeRSS Feed
drajabhathor
Calcite | Level 5

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.

 

data do_while_data;
y=0;
do i= 1 to 5 by 0.5 while (y<20);
y=i**2; /*values are 1, 2.25, 4, 6.25,...,16*/
output;
end;
run;
proc print data=do_while_data;
run;

 

Here is my output data:

 

Screen Shot 2016-06-20 at 4.16.58 PM.png

 

Thank you!!!

2 REPLIES 2
Reeza
Super User

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.

 

 

http://support.sas.com/documentation/cdl/en/lestmtsref/68024/HTML/default/viewer.htm#p1awxgleif5wlen...

 

In your case, when it enters the loop its 16, at the end it's 20.25 so it doesn't iterate again. 

 

Astounding
PROC Star

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.

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
  • 2 replies
  • 685 views
  • 0 likes
  • 3 in conversation