BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

Hello,

Why is the value of index (i) 2 in below two programs since the loop iterates just only for once?

 

data have;
	input x y;
	cards;
1 2
;

data want;
	set have;
	array ar{*} x y;

	do i = 1 to dim(ar)-1;
		put i =;
		var = ar{i+1}-ar{i};
	end;

	put i =;
run;

data have;
	input x y;
	cards;
1 2
;

data want;
	set have;
	array ar{*} x y;

	do i = 1 to dim(ar)-1;
		put i =;
		var = ar{i}-ar{i};
	end;

	put i =;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

A do loop works like that:

 

1 - set index variable to initial value

2 - compare index variable with termination value

3 - if higher goto 7

4 - execute loop body

5 - increment index value

6 - goto 2

7 - end

 

So you see that the loop terminates only when the index variable exceeds the termination value.

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

When i increases to be > dim(ar)-1, the statements inside the loop are not executed. So i increases to be equal to 2, but i is never equal to two inside the loop. Run this program to verify

 

data have;
	input x y;
	cards;
1 2
;

data want;
	set have;
	array ar{*} x y;

	do i = 1 to (dim(ar)-1);
		put i=;
		var = ar{i+1}-ar{i};
	end;
	
run;
Kurt_Bremser
Super User

A do loop works like that:

 

1 - set index variable to initial value

2 - compare index variable with termination value

3 - if higher goto 7

4 - execute loop body

5 - increment index value

6 - goto 2

7 - end

 

So you see that the loop terminates only when the index variable exceeds the termination value.

SAS_inquisitive
Lapis Lazuli | Level 10

@Kurt_Bremser What are numeric values 7 and 2?

art297
Opal | Level 21

The index of a do loop, by definition, increases until it exceeds it's "to" value. When it does exceed that value, it exits from the loop with processing the commands within the loop.

 

Art, CEO, AnalystFinder.com

 

SAS_inquisitive
Lapis Lazuli | Level 10
Thank you all for the explanation.

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