I am reviewing for the Base SAS certification and was questioning one of the answers in the book. The question is:
In the following program, complete the statement so that the program stops generating observations when Distance reaches 250 miles or when 10 gallons of fuel have been used.
data work.go250;
set cert.cars;
do gallons=1 to 10 ... ;
Distance=gallons*mpg;
output;
end;
run;
The correct answer, according to the book, is while(Distance<=250). However, I believe the answer is until(Distance=250).
My understanding of the WHILE expression is that it is evaluated before the execution of the DO loop, so if Distance reaches 250, the do loop will still execute because the statement (Distance <= 250) is still true. I made up a dataset in SAS Studio to see what would happen with both the WHILE and UNTIL statements.
WHILE statement
data cars;
input mpg;
datalines;
50
;
run;
data work.go250;
set cars;
do gallons=1 to 10 while(Distance le 250);
Distance=gallons*mpg;
output;
end;
run;
proc print data = go250; run;
Obs mpg gallons Distance
1 50 1 50
2 50 2 100
3 50 3 150
4 50 4 200
5 50 5 250
6 50 6 300
UNTIL statement
data work.go250_2;
set cars;
do gallons=1 to 10 until(Distance=250);
Distance=gallons*mpg;
output;
end;
run;
proc print data = go250_2; run;
Obs mpg gallons Distance
1 50 1 50
2 50 2 100
3 50 3 150
4 50 4 200
5 50 5 250
Based on the output, it looks like the UNTIL statement satisfies the requirement of "...Distance reaches 250 miles..."
Any insights would be appreciated. Thanks!