- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data WORK.LOOP;
X = 0;
do Index = 1 to 5 by 2;
X = Index;
end;
run;
Q) Upon completion of execution, what are the values of the variables X and Index in the SAS data set
named WORK.LOOP?
The answer given is X = 5, Index = 7
Can someone kindly enlighten me?
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The do loop
DO index=1 to 5 by 2;
x=index;
end;
tells SAS to
- Assess INDEX at the top of the loop to see whether index satisfies the condition needed to run another iteration.
- Increments INDEX by 2 at the bottom of the loop.
So when index=5, the loop runs an iteration (and x=5). At the end of that iteration INDEX is incremented to 7, then SAS goes to the top for assessment of index. It exceeds 5, so the do loops are finished, leaving X=5.
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set
Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets
--------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Using put statements carefully between statements helps us to know what's happening during each iteration and why(logic aka the loop's exit)
data WORK.LOOP;
X = 0;
do Index = 1 to 5 by 2;
X = Index;
put x= index=;
end;
put index=;
run;
put x= index=;-->X=1 Index=1
put x= index=;-->X=3 Index=3
put x= index=;-->X=5 Index=5
put index=; -->Index=7 loops exit
Does this help at all?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@cow240wrote:
Thank you for your replies. Apologies for the newbie questions as im new to SAS. I just thought that intuitively, it should stop at 5 since it exits at 5. I guess I have a lot to learn!
No need for Apologies . There aint somebody who is more dumb than me when i started learning sas. Cheers!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The do loop
DO index=1 to 5 by 2;
x=index;
end;
tells SAS to
- Assess INDEX at the top of the loop to see whether index satisfies the condition needed to run another iteration.
- Increments INDEX by 2 at the bottom of the loop.
So when index=5, the loop runs an iteration (and x=5). At the end of that iteration INDEX is incremented to 7, then SAS goes to the top for assessment of index. It exceeds 5, so the do loops are finished, leaving X=5.
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set
Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets
--------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The question is very trivial and basic, however you @cow240 could mark one of the answer as accepted and answered