BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cow240
Calcite | Level 5

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?

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

The do loop

 

   DO index=1 to 5 by 2;

    x=index;

   end;

 

tells SAS to

  1. Assess INDEX at the top of the loop to see whether index satisfies the condition needed to run another iteration.
  2. 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

--------------------------

View solution in original post

6 REPLIES 6
LinusH
Tourmaline | Level 20
Run the step yourself and insert PUT statements or using data step debugger to follow variable value assignments.
Data never sleeps
novinosrin
Tourmaline | Level 20

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?

cow240
Calcite | Level 5
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!
novinosrin
Tourmaline | Level 20

@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!

mkeintz
PROC Star

The do loop

 

   DO index=1 to 5 by 2;

    x=index;

   end;

 

tells SAS to

  1. Assess INDEX at the top of the loop to see whether index satisfies the condition needed to run another iteration.
  2. 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

--------------------------
MarkWik
Quartz | Level 8

The question is very trivial and basic, however you @cow240 could mark one of the answer as accepted  and answered

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1827 views
  • 0 likes
  • 5 in conversation