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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.