BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
vijaypratap0195
Obsidian | Level 7

Explain why I am getting all the EVEN values when I am doing i+j as a final result in the nested loop:

 

data floyd;
array arr(*) col1-col5;
do i=1 to 5;
	do j=1 to i;
		arr(i) = i+j; 
	end;	
	output;
end;
drop i j;
run;
proc print; run;

vijaypratap0195_0-1693674912395.png

As per my understanding output has to be :

Expected output:

vijaypratap0195_1-1693675160236.png

and so on.........

Can anyone please explain.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

No.

You seem to have confused which index variable represents the observation number (aka ROW) and which one represents the variable number (aka COLUMN).

 

It would also be clearer to empty the array after you write it to see what you are really doing.

data floyd;
array arr[*] col1-col5;
do i=1 to 5;
	do j=1 to i;
		arr[i] = i+j; 
	end;	
	output;
  call missing(of arr[*]);
end;
drop i j;
run;

Result

Obs    col1    col2    col3    col4    col5

 1       2       .       .       .       .
 2       .       4       .       .       .
 3       .       .       6       .       .
 4       .       .       .       8       .
 5       .       .       .       .      10

So when I=2 you write two different values into ARR[2], so only the final one, 4, remains.

 

It becomes clearer is you use ROW and COL as the index variable names.

Now it become clearer that you wanted:

arr[col] = row+col;

Instead of

arr[row] = row+col;

Try

data floyd;
array arr[*] col1-col5;
do row=1 to 5;
	do col=1 to row;
		arr[col] = row+col; 
	end;	
	output;
end;
drop row col;
run;
proc print; run;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

No.

You seem to have confused which index variable represents the observation number (aka ROW) and which one represents the variable number (aka COLUMN).

 

It would also be clearer to empty the array after you write it to see what you are really doing.

data floyd;
array arr[*] col1-col5;
do i=1 to 5;
	do j=1 to i;
		arr[i] = i+j; 
	end;	
	output;
  call missing(of arr[*]);
end;
drop i j;
run;

Result

Obs    col1    col2    col3    col4    col5

 1       2       .       .       .       .
 2       .       4       .       .       .
 3       .       .       6       .       .
 4       .       .       .       8       .
 5       .       .       .       .      10

So when I=2 you write two different values into ARR[2], so only the final one, 4, remains.

 

It becomes clearer is you use ROW and COL as the index variable names.

Now it become clearer that you wanted:

arr[col] = row+col;

Instead of

arr[row] = row+col;

Try

data floyd;
array arr[*] col1-col5;
do row=1 to 5;
	do col=1 to row;
		arr[col] = row+col; 
	end;	
	output;
end;
drop row col;
run;
proc print; run;
vijaypratap0195
Obsidian | Level 7

Very depth explanation, Thank you.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 2 replies
  • 903 views
  • 1 like
  • 2 in conversation