BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sathiskumar_D
Obsidian | Level 7
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??????

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

 

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

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.

 

Sathiskumar_D
Obsidian | Level 7

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

kaurbal
Calcite | Level 5

Thanks for explaining.

ballardw
Super User

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

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 4 replies
  • 4046 views
  • 1 like
  • 4 in conversation