Contributor
Posts: 60

# matrix creation

I do not have SAS IML. I am trying to figure out how to create a matrix with base SAS. Does someone have a smart code for this. I was thinking about a double loop..

SAS Super FREQ
Posts: 4,248

## matrix creation

You can create it using the ARRAY statement in the DATA step:

data m(drop=i j);

array x[3];

do i = 1 to 5;

do j = 1 to dim(x);

x = ranuni(1);

end;

output;

end;

run;

proc print; run;

However, it's not clear what you're going to be able to do with it.

To find out some of the things that you can do with SAS/IML see the Getting Started articles.

Also see the book Statistical Programming with SAS/IML Software.

PROC Star
Posts: 8,170

## matrix creation

Rick: Do I happen to all of the sudden see colored code in your posts?

SAS Super FREQ
Posts: 4,248

:smileygrin:

Contributor
Posts: 60

## matrix creation

Rick,

Thanks for the code. In term of memory , does working with an array is more efficient than with a nested loop as Art suggested?

SAS Super FREQ
Posts: 4,248

## matrix creation

Art notes that the ARRAY statement can have multiple dimension (for example, two instead of the one-dimensional array that I used).

If you are going to try to use the DATA step to manipulate an array, a 2D array probably makes more sense. If your goal is to write a matrix to a data set, then my code is adequate and uses less memory because only one row of the matrix is ever in RAM.

Contributor
Posts: 60

## matrix creation

I do need a 2d array, I would like to manipulate the array and I am kind of new to array in SAS. One more question if you don't mind.

Let's say i want to fill the array,  the cell of row 2 and column 2 should be equal 0.8. How do I write this in SAS?

Posts: 1,318

## Re: matrix creation

data foobar;

array x[3] (3*0);

do i=1 to 5;

do j=1 to dim(x);

x=ifn(i=2 and j=2,.8,ranuni(1));

end;

output;

end;

drop i j;

run;

proc print data=foobar; run

`Obs x1 x2 x31 0.18496 0.97009 0.399822 0.25940 0.80000 0.969283 0.54298 0.53169 0.049794 0.06657 0.81932 0.523875 0.85339 0.06718 0.95702`
Contributor
Posts: 60

Thanks so much!

PROC Star
Posts: 8,170