turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Distinguish between elements in a two-dimensional ...

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

12-03-2009 02:27 AM

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?

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?

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to deleted_user

12-03-2009 08:12 AM

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;

%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;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Flip

12-03-2009 12:30 PM

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

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