DATA Step, Macro, Functions and more

DO loop basics

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

DO loop basics

data WORK.LOOP;
  X = 0;
  do Index = 1 to 5  by  2;
    X = Index;
  end;
run;

Upon completion of execution, what are the values of the variables X and Index in the SAS data set named WORK.LOOP?

  1. X = 3, Index = 5
  2. X = 5, Index = 5
  3. X = 5, Index = 6
  4. X = 5, Index = 7

I knew the answer was X=5 and index=7;

Question is : When the index =5, isn't  the program stop executing DO loop and gives the answer of index=5 and x=5.However, the answer was X=5 and index=.How??????


Accepted Solutions
Solution
‎05-08-2017 08:32 AM
Super User
Posts: 7,766

Re: DO loop basics

Posted in reply to Sathiskumar_D

A do loop works like that:

1 - set the iteration variable to the start value

2 - compare to the end value

3 - if less or equal to the end value (greater or equal if the by value is negative), enter the body of the do, otherwise goto 6

4 - at the end of the body, increment by the by value

5 - goto 2

6 - end

 

So you see that the iteration variable needs to exceed the end value to terminate the iterative do loop.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎05-08-2017 08:32 AM
Super User
Posts: 7,766

Re: DO loop basics

Posted in reply to Sathiskumar_D

A do loop works like that:

1 - set the iteration variable to the start value

2 - compare to the end value

3 - if less or equal to the end value (greater or equal if the by value is negative), enter the body of the do, otherwise goto 6

4 - at the end of the body, increment by the by value

5 - goto 2

6 - end

 

So you see that the iteration variable needs to exceed the end value to terminate the iterative do loop.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 12

Re: DO loop basics

Posted in reply to KurtBremser

Thanks mr.KurtBremser.I am still in the learning curve.

Super User
Posts: 11,343

Re: DO loop basics

Posted in reply to Sathiskumar_D

For added education use BY 3; and predict the values.

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 338 views
  • 1 like
  • 3 in conversation