When I executed codes below, I got 11 in variable i. Why it is not 10? Could you kindly explain a little more regarding to how SAS works in this case? Thanks!
data a;
do i = 1 to 10;
end;
output;
run;
proc print data=a;
run;
The do loop value is incremented by the step value (default is 1) at the encounter of an end statement. Therefore the end result of your code is one more than the do loop, and that is output. The code within the do loop is only executed until incrementor is above the high limit.
You can read the documentation on do loops:
i is set to 1, and 1 is less than or equal to 10 so the loop continues
....
i is set to 10, and 10 is less than or equal to 10 so the loop continues
i is set to 11, and 11 is not less than or equal to 10 so the code in the loop is not executed and SAS goes on to the next statement.
i is 11 at output time.
While the first couple of responses outline the rules, here are a couple of DATA steps you can play with to try to get the idea:
data _null_;
do k=1 to 7 by 4;
put k=;
end;
put k=;
run;
data _null_;
do k=1 to 9 by 4;
put k=;
k = k + 1;
put k=;
end;
put k=;
run;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.