DATA Step, Macro, Functions and more

index value in array

Accepted Solution Solved
Reply
Super Contributor
Posts: 271
Accepted Solution

index value in array

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;

 


Accepted Solutions
Solution
‎06-19-2017 09:33 AM
Super User
Posts: 7,809

Re: index value in array

Posted in reply to SAS_inquisitive

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
PROC Star
Posts: 754

Re: index value in array

Posted in reply to SAS_inquisitive

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;
Solution
‎06-19-2017 09:33 AM
Super User
Posts: 7,809

Re: index value in array

Posted in reply to SAS_inquisitive

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super Contributor
Posts: 271

Re: index value in array

Posted in reply to KurtBremser

@KurtBremser What are numeric values 7 and 2?

Super User
Posts: 7,809

Re: index value in array

Posted in reply to SAS_inquisitive

These are just "line numbers" similar to those used in eg BASIC, to identify places in the code.


SAS_inquisitive wrote:

@KurtBremser What are numeric values 7 and 2?


 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
PROC Star
Posts: 7,474

Re: index value in array

[ Edited ]
Posted in reply to SAS_inquisitive

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

 

Super Contributor
Posts: 271

Re: index value in array

Posted in reply to SAS_inquisitive
Thank you all for the explanation.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 200 views
  • 3 likes
  • 4 in conversation