Still trying to learn SAS and came across an interesting question/sample of code
This is the sample question/code
data WORK.LOOP;
X = 0;
do Index = 1 to 5 by 3;
X = Index;
end;
run;
So I can see that the final output is
X=4 Index=7
I understand that the Index starts at 1, then increments by 3 and so forth. So technically the index output should be
Index=1
Index=4
This is where things get muddy for me.
If Index = 4, and the next value is greater then 5 I would have thought the final output would be
X=4 Index=4
Why would this not be the final output?
Hope this makes sense, been quite a long day reading through stuff
this example has the whys included.
data WORK.LOOP;
X = 0;
do Index = 1 to 5 by 3;
/* the variable x is set at this point only if the condition is met*/
X = Index;
output;
end;
/* while the variable xy is set at this point after the do loop is completed */
xy=index;
output;
run;
data WORK.LOOP2;
X = 0;
do Index = 1 to 5 by 2;
/* the variable x is set at this point only if the condition is met*/
X = Index;
output;
end;
/* while the variable xy is set at this point after the do loop is completed */
xy=index;
output;
run;
It increments and then tests.
An iterative do loop is in fact a do while:
So the loop will only end when the index has gone past the end value.
because of the by 3 statement.
change the by 3 to by 2 and it may make since to you. Put a debugger into the datastep maybe like this.
data WORK.LOOP / debug;
X = 0;
do Index = 1 to 5 by 3;
X = Index;
end;
run;
then try this
data WORK.LOOP / debug;
X = 0;
do Index = 1 to 5 by 2;
X = Index;
end;
run;
this example has the whys included.
data WORK.LOOP;
X = 0;
do Index = 1 to 5 by 3;
/* the variable x is set at this point only if the condition is met*/
X = Index;
output;
end;
/* while the variable xy is set at this point after the do loop is completed */
xy=index;
output;
run;
data WORK.LOOP2;
X = 0;
do Index = 1 to 5 by 2;
/* the variable x is set at this point only if the condition is met*/
X = Index;
output;
end;
/* while the variable xy is set at this point after the do loop is completed */
xy=index;
output;
run;
Thanks for all the suggestions!
I actually tried the DEBUG step, but I am currently using SAS Studio online and hit an error with DEBUG saying it could not initialize.
FYI for anyone else that comes across this thread please note this.
https://communities.sas.com/t5/SAS-Analytics-U/Debug-option-in-Data-Step-does-not-work/td-p/197405
Though I never knew that SAS had a DEBUG option.
This makes more sense now, thanks everyone!
While @Kurt_Bremser gave you the details, here is a simplified way of looking at it.
How does INDEX go from 1 to 5?
At the END statement, SAS adds 3 to the value of INDEX. Then it evaluates, "Are we done yet?"
Since 1 + 3 = 4 is less than 5 (the upper range of the loop), the loop executes a second time.
Once again, at the END statement, SAS adds 3 to the value of INDEX. Then it evalutes, "Are we done yet?"
Since 4 + 3 = 7 is greater than 5 (the upper range of the loop), the loop is now over (with INDEX equal to 7).
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.