BookmarkSubscribeRSS Feed
stevo642
Obsidian | Level 7

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.

3 REPLIES 3
Rick_SAS
SAS Super FREQ

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?

 

Rick_SAS
SAS Super FREQ

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;

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Multiple Linear Regression in SAS

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.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 3 replies
  • 893 views
  • 1 like
  • 2 in conversation