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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

6 REPLIES 6
LinusH
Tourmaline | Level 20
Run the step yourself and insert PUT statements or using data step debugger to follow variable value assignments.
Data never sleeps
novinosrin
Tourmaline | Level 20

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?

cow240
Calcite | Level 5
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!
novinosrin
Tourmaline | Level 20

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

mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
MarkWik
Quartz | Level 8

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

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
  • 6 replies
  • 893 views
  • 0 likes
  • 5 in conversation