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

Hello,

 

I was provided with this practise exam question (image below) and cannot understand why the given answer is not:

Total B=3; GrandTot=104.  Indeed the answer given was Total B=4; GrandTot=104 which is confusing me.

After the loop has completed, wouldn't Total B = 3? The Grand total will then be 101+3=104?

Would be most grateful for some advice on this.

Thank you kindly.

 

sas_ds2_loop_question_image.jpg

1 ACCEPTED SOLUTION

Accepted Solutions
RichardDeVen
Barite | Level 11

That is expected behavior.   When the index variable is step adjusted to fall outside the DO range the loop is terminated.  In this case the step adjustment (BY value) is implicitly +1 because there is no explicit BY.

 

The GRANDTOT computation is done before the DO statement steps in the index variable TOTALB past the TO value (3), So you get GRANDTOT 104 and TOTALB 4.

 

The DS2 "DO Statement" reference documentation for TO stop covers the concept in detail:

Execution continues based on the value of increment until one of the following conditions is met: the value of index-variable passes the value of stop, ...

FWIW, horrible coding in this practice question, would not pass code review.

View solution in original post

2 REPLIES 2
RichardDeVen
Barite | Level 11

That is expected behavior.   When the index variable is step adjusted to fall outside the DO range the loop is terminated.  In this case the step adjustment (BY value) is implicitly +1 because there is no explicit BY.

 

The GRANDTOT computation is done before the DO statement steps in the index variable TOTALB past the TO value (3), So you get GRANDTOT 104 and TOTALB 4.

 

The DS2 "DO Statement" reference documentation for TO stop covers the concept in detail:

Execution continues based on the value of increment until one of the following conditions is met: the value of index-variable passes the value of stop, ...

FWIW, horrible coding in this practice question, would not pass code review.

Daniel121181
Fluorite | Level 6

Thank you very much for your answer Richard and apologies for the delay in replying.

 

This is a knowledge-sharing community for learners in the Academy. Find answers to your questions or post here for a reply.
To ensure your success, use these getting-started resources:

Estimating Your Study Time
Reserving Software Lab Time
Most Commonly Asked Questions
Troubleshooting Your SAS-Hadoop Training Environment

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 680 views
  • 0 likes
  • 2 in conversation