Statistical programming, matrix languages, and more

How to loop over a list of stored matrices to sequentially load and process them

Reply
Occasional Contributor
Posts: 14

How to loop over a list of stored matrices to sequentially load and process them

I have several stored matrices named like: DATA01  DATA02  DATA03  DATA04  DATA05... DATA20

 

I'd like to load and process them one at a time in a loop and I'd like to do this all in IML -- no macro code.

 

In pseudocode it would look like:

 

DO i = 1 TO 20;    *** might have to do two loops, one for 01-09 and another for 10-20;

 

   LOAD DATAi;

 

   PRINT "SUM = " (DATA0i[+]);  *** a simple calc;

 

END;

 

I've got the matrix names stored in an array but I don't know how to evaluate the array elements in the loop so they resolve to DATA01, etc.

 

Any tips & suggestions are appreciated.

SAS Super FREQ
Posts: 3,225

Re: How to loop over a list of stored matrices to sequentially load and process them

Are the matrices the same dimension?   If so, the easiest way to process is to store them all in a data set and use an ID variable or a READ NEXT statement to process each matrix.

 

If you have to read 20 matrices that are stored, do yo uknow that there are 20 and that the matrix names are data1-data20?

 

SAS Super FREQ
Posts: 3,225

Re: How to loop over a list of stored matrices to sequentially load and process them

The loading is easy because you can use 

load _all_;

to read all matrices. Otherwise, explicitly name the matrices:.

load data1 data2;

 

The easiest way to process each matrix in a loop is to use the VALUE function to retrieve the matrix that is associated with each name. See the article "Indirect assignment: How to create and use matrices named x1, x2, ..., xn."  Here is an example:

 

/* store some matrices */
proc iml;
data1 = I(2);
data2 = I(3);
store data1 data2;
quit;


proc iml;
load _all_;  /* or load data1 data2; */
show names;

do i = 1 to 2;
   matname  = "data" + strip(char(i));
   m = value(matname);
   PRINT "SUM = " (m[+]);  *** a simple calc;
end;

 

Occasional Contributor
Posts: 14

Re: How to loop over a list of stored matrices to sequentially load and process them

VALUE() works great, thank you!
Post a Question
Discussion Stats
  • 3 replies
  • 147 views
  • 1 like
  • 2 in conversation