DATA Step, Macro, Functions and more

Do Loop

Accepted Solution Solved
Reply
Highlighted
New Contributor
Posts: 2
Accepted Solution

Do Loop

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
Solution
Wednesday
Trusted Advisor
Posts: 1,282

Re: Do Loop

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.

View solution in original post


All Replies
Super User
Posts: 5,824

Re: Do Loop

Run the step yourself and insert PUT statements or using data step debugger to follow variable value assignments.
Data never sleeps
PROC Star
Posts: 1,287

Re: Do Loop

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?

New Contributor
Posts: 2

Re: Do Loop

Posted in reply to novinosrin
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!
PROC Star
Posts: 1,287

Re: Do Loop


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

Solution
Wednesday
Trusted Advisor
Posts: 1,282

Re: Do Loop

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.

Frequent Contributor
Posts: 97

Re: Do Loop

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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