BookmarkSubscribeRSS Feed
sasuser1000
Calcite | Level 5

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..

9 REPLIES 9
Rick_SAS
SAS Super FREQ

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.

art297
Opal | Level 21

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

Rick_SAS
SAS Super FREQ

:smileygrin:

sasuser1000
Calcite | Level 5

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?

Rick_SAS
SAS Super FREQ

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.

sasuser1000
Calcite | Level 5

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?

FriedEgg
SAS Employee

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

sasuser1000
Calcite | Level 5

Thanks so much!

art297
Opal | Level 21

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 1006 views
  • 6 likes
  • 4 in conversation