BookmarkSubscribeRSS Feed
swayto
Fluorite | Level 6

Data Loop;
X=0;
do Index = 1 to 5 by 2;
x=Index;
end;
run;

 

X=5 , Index=7

 

Why is index=7.

3 REPLIES 3
gamotte
Rhodochrosite | Level 12

Hello,

 

When index=5 and the interpreter meets the "end" statements, il loops back to the do

statement, increments index by 2 and compare the result to 5 => index=7>5 so the loop stops.

andreas_lds
Jade | Level 19

Adding a put statement in the loop could be helpful to understand what's happening.

Kurt_Bremser
Super User

A loop like yours is compiled to this (and the same is true for any programming language that has an iterative loop):

index = 1
start:
if index > 5 goto end
(loop code)
index + 2
goto start
end:

so you see that the index needs to go past the end value for the loop to exit.

 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 468 views
  • 0 likes
  • 4 in conversation