DATA Step, Macro, Functions and more

matrix creation

Reply
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: 3,488

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: 7,366

matrix creation

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

SAS Super FREQ
Posts: 3,488

matrix creation

: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: 3,488

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?

Trusted Advisor
Posts: 1,300

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 x3

1 0.18496 0.97009 0.39982

2 0.25940 0.80000 0.96928

3 0.54298 0.53169 0.04979

4 0.06657 0.81932 0.52387

5 0.85339 0.06718 0.95702

Contributor
Posts: 60

Re: matrix creation

Thanks so much!

PROC Star
Posts: 7,366

matrix creation

Depending upon what you want to do with the matrix, you can always create a multi-dimensional array in SAS and fill it either with nested do loops are simply by initializing the cells with specific data.

Ask a Question
Discussion stats
  • 9 replies
  • 171 views
  • 6 likes
  • 4 in conversation