Data Loop;
X=0;
do Index = 1 to 5 by 2;
x=Index;
end;
run;
X=5 , Index=7
Why is index=7.
Hello,
When index=5 and the interpreter meets the "end" statements, il loops back to the do
statement, increments index by 2 and compare the result to 5 => index=7>5 so the loop stops.
Adding a put statement in the loop could be helpful to understand what's happening.
A loop like yours is compiled to this (and the same is true for any programming language that has an iterative loop):
index = 1 start: if index > 5 goto end (loop code) index + 2 goto start end:
so you see that the index needs to go past the end value for the loop to exit.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.