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.
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?
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;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.