Reputation: 71
data primes;
length status $12.;
do i=3 to 6;
status='Prime';
do j=2 to i-1;
if mod(i, j) = 0 then do;
status='Composite';
leave; *exit loop;
end;
end;
Output;
end;
run;
proc print data = primes;
run;
I wrote this code and got the output as below. can someone please explain how the value of j is being picked here? How can j=i in output when the loop goes till j=i-1
Obs status i j
1 Prime 3 3
2 Composite 4 2
3 Prime 5 5
4 Composite 6 2
Upvotes: 0
Views: 39
Reputation: 9109
I has to do with the way the loop is stopped. The check is done at the top of the loop after the index variable is incremented. If it is greater than the stop value the loop stops. You could stop your loop with until(j eq i-1) and see the value you expect. The reason the DO loop uses GT stop is because the increment may never have the exact value of stop.
Also note this is all in the book. DO Statement, Iterative
Upvotes: 3