Statistical programming, matrix languages, and more

Loop over names with a suffix in Sas in proc iml

Occasional Learner
Posts: 1

Loop over names with a suffix in Sas in proc iml

[ Edited ]

Hello all,


I have 10 datasets going from input1 to input10. Then I want to transform them to iml data so I can do complex matrix calculations. The problem is that I can loop over the datasets with &i. as the variable suffix part. But I do not know how to loop over them when they are iml data, is it possible to do this in iml or is it just not possible?


proc iml;


%do i=1 %to 10;

use input&i.;

read all var _ALL_ into imlData&i.[colname=varNames];

close input&i.;






%The code below does not work !!

do i =1 to 10;

imlData&i.[1,1] = assign a new complex value (not important);



If anybody knows the answer please tell me Smiley Happy, thank you all!

Frequent Contributor
Posts: 143

Re: Loop over names with a suffix in Sas in proc iml

Have a look at CALL EXECUTE as you can use this to run IML statements that you have created in a character variable.  For example:


do i = 1 to 10;
  call execute( 'imlData' + strip(char(i)) + '[1,1] = 2#i;' );

This should do what you want, but the problem is that a real program with a lot of CALL EXECUTEs can be difficult to read and to maintain, so you might be better off keeping all your data in one large matrix and then access individual matrices by subsetting rows and columns.



Note that you must use CALL EXECUTE inside an IML module.

Posts: 3,624

Re: Loop over names with a suffix in Sas in proc iml

[ Edited ]

The answer depends on what you want to do with the data.

1. Do you want to analyze each data set separately? (This is the most common wish.) Then you can read the data set names from an array like this:

proc iml;
dsName = "input1":"input10";
do i = 1 to ncol(dsName);
   use (dsName[i]);
   read all var _ALL_ into X;
   close (dsName[i]);
   /* do a computation for this data set */


2. Do you want to read all of the data into IML matrices and then do computations across data sets? (This is rare.) If so, you can follow the directions in this article about reading data into separate matrices.


In either case, you don't need any macro code, so get rid of the %DO statements and replace them with SAS/IML DO loops.

Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation