SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rodneyc8063
Fluorite | Level 6

Still trying to learn SAS and came across an interesting question/sample of code

 

This is the sample question/code

 

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

 

So I can see that the final output is

 

X=4 Index=7

 

I understand that the Index starts at 1, then increments by 3 and so forth. So technically the index output should be

 

Index=1

Index=4

 

This is where things get muddy for me.

 

If Index = 4, and the next value is greater then 5 I would have thought the final output would be

 

X=4 Index=4

 

Why would this not be the final output?

 

Hope this makes sense, been quite a long day reading through stuff

1 ACCEPTED SOLUTION

Accepted Solutions
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

this example has the whys included.

 

data WORK.LOOP;
X = 0;
do Index = 1 to 5 by 3;
/* the variable x is set at this point only if the condition is met*/
X = Index;output;
end;
/* while the variable xy is set at this point after the do loop is completed */
xy=index;output;
run;




data WORK.LOOP2;
X = 0;
do Index = 1 to 5 by 2;
/* the variable x is set at this point only if the condition is met*/
X = Index;output;
end;
/* while the variable xy is set at this point after the do loop is completed */
xy=index;output;
run;

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

An iterative do loop is in fact a do while:

  1. set index to start value
  2. if index greater end value, leave
  3. execute loop code
  4. increment index
  5. go to 2.

So the loop will only end when the index has gone past the end value.

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

because of the by 3 statement.  

change the by 3 to by 2 and it may make since to you.  Put a debugger into the datastep maybe like this.

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


then try this

data WORK.LOOP / debug;
X = 0;
do Index = 1 to 5 by 2;
X = Index;
end;
run;
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

this example has the whys included.

 

data WORK.LOOP;
X = 0;
do Index = 1 to 5 by 3;
/* the variable x is set at this point only if the condition is met*/
X = Index;output;
end;
/* while the variable xy is set at this point after the do loop is completed */
xy=index;output;
run;




data WORK.LOOP2;
X = 0;
do Index = 1 to 5 by 2;
/* the variable x is set at this point only if the condition is met*/
X = Index;output;
end;
/* while the variable xy is set at this point after the do loop is completed */
xy=index;output;
run;
rodneyc8063
Fluorite | Level 6

Thanks for all the suggestions!

 

I actually tried the DEBUG step, but I am currently using SAS Studio online and hit an error with DEBUG saying it could not initialize.

 

FYI for anyone else that comes across this thread please note this.

 

https://communities.sas.com/t5/SAS-Analytics-U/Debug-option-in-Data-Step-does-not-work/td-p/197405

 

Though I never knew that SAS had a DEBUG option.

 

This makes more sense now, thanks everyone!

Astounding
PROC Star

While @Kurt_Bremser gave you the details, here is a simplified way of looking at it.

 

How does INDEX go from 1 to 5?

 

At the END statement, SAS adds 3 to the value of INDEX.  Then it evaluates, "Are we done yet?"

 

Since 1 + 3 = 4 is less than 5 (the upper range of the loop), the loop executes a second time.

 

Once again, at the END statement, SAS adds 3 to the value of INDEX.  Then it evalutes, "Are we done yet?"

 

Since 4 + 3 = 7 is greater than 5 (the upper range of the loop), the loop is now over (with INDEX equal to 7).

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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