- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
It increments and then tests.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
An iterative do loop is in fact a do while:
- set index to start value
- if index greater end value, leave
- execute loop code
- increment index
- go to 2.
So the loop will only end when the index has gone past the end value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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).