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

The following SAS program is submitted: 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?

A. X = 3, Index = 5

B. X = 5, Index = 5

C. X = 5, Index = 6

D. X = 5, Index = 7

 

Why the correct_answer = "D"?? I thought index should never exceed 5?

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Yes.  At the end of the third iteration, once X=7, SAS notices that 7 > 5 so the loop must be complete.

View solution in original post

5 REPLIES 5
Astounding
PROC Star

Just the opposite ... the final value of INDEX will always exceed 5.

 

The key issue is how INDEX gets incremented.  Each time the loop reaches the END statement, it adds 2 to the value of INDEX.  Then it considers, "Am I done yet?"  The criterion that it uses to determine whether the loop is done:  does INDEX now exceed the upper bound of 5.

pchen002
Obsidian | Level 7

Oh so it means if X=1, index=3, X=3, index=5, X=5,index=7? Sorry just started learning SAS, pardon if I am weak at my foundation, thanks!!

Astounding
PROC Star

Yes.  At the end of the third iteration, once X=7, SAS notices that 7 > 5 so the loop must be complete.

ChrisBrooks
Ammonite | Level 13

This looks a lot like homework 😉 but still....

 

In this form of the Do statement execution of the step continues until the value of the index exceeds the stop value - you can find more here http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000201276.htm

ballardw
Super User

1) Incomplete data step

 

Iterated do loops: ie. Do var = 1 to value (by otherval)

always interate until the value limit is exceeded. At which point the the code inside the loop is not executed.

 

It is similar to Do while (x < 10);

                       x+1;

                       end;

 

the condition is true until x is greater than or equal to 10. At which time x is larger than the loop limit, the loop doesn't execute but the value of x is retained.

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 3418 views
  • 3 likes
  • 4 in conversation