Not applicable
Posts: 0

# Distinguish between elements in a two-dimensional array.

If you declare

array x(2,5);

in a data step and assign values to the array, you get x1-x10 in the resulting table. The logical result would have been x11 to x15 and x21 to x25.

By declaring

array x(2,5) a1b1-a1b5 a2b1-a2b5;

you can get what you want.

But if it had been x(100,5)?

By the way: Are expressions like

array y(2,&n) r1c1-r1c&n r2c1-r2c&n;

allowed?
Super Contributor
Posts: 359

## Re: Distinguish between elements in a two-dimensional array.

Here is a simple example of creating a 2 dimensional array.

%macro test;
%let i = 20;
%let j = 100;
data one;
array newv (&i, &j) %do k = 1 %to &i ; var_&k._1 - var_&k._&j %end; ;
do m = 1 to &i;
do i = 1 to &j ;
newv(m, i) = m*i;
end;
end;
run;
%mend;
%test;
Super Contributor
Posts: 474

## Re: Distinguish between elements in a two-dimensional array.

Memory wise everything is stored contiguously, the two dimensions are just a pictorial view of the vector in a tabular form.

Another way (beside the explicit association of named variables) would be to transform the two dimension address to a single one.

[pre]d1,d2 = (d1-1)*max(d2)+d2[/pre]
so, for a 2,5 vector, for example:

1,2 becomes (1-1)*5+2=2, thus X2
1,5 becomes (1-1)*5+5=5, thus X5
2,3 becomes (2-1)*5+3=8, thus X8
2,5 becomes (2-1)*5+5=10, thus X10

you could code this into a macro like this:

[pre]%macro i(D1,D2,MAX_D2);
%sysevalf( (&D1-1)*&MAX_D2+&D2)
%mend i;[/pre]
then you just have to reference each variable using the macro, this way:

[pre]X%i(1,2,5)=10000; * which resolves to X2;
X%i(1,5,5)=10001; * which resolves to X5;
X%i(2,3,5)=10002; * which resolves to X8;
X%i(2,5,5)=10003; * which resolves to X10;[/pre]
Cheers from Portugal

Daniel Santos @ www.cgd.pt
Discussion stats
• 2 replies
• 132 views
• 0 likes
• 3 in conversation