Statistical programming, matrix languages, and more

matrix allocation

Reply
Occasional Contributor
Posts: 17

matrix allocation

Hi,

I am working with IML and I want to obtain a matrix of coefficient by assigning element by element something like this :
%macro cal(arguments);
.
.
.
%do ind=1 %to n;
%do sim=1 %to p;
.
.
.
proc iml;
.
.
.
RVCC[&ind,∼]=trace(s)/sqrt(trace(w**2)*trace(w2**2));
.
.
%end;
%end;
.
.
.
%mend


I have this error : "Matrix RVCC has not been set to a value"

Why do this occur?! Any one can help me?


Sincerly,

Nad
SAS Employee
Posts: 94

Re: matrix allocation

You need to allocate the RVCC matrix before you assign to it. You can do that with a statement of the form:

RVCC = j(num_rows,num_cols);

You will need to figure out what num_rows and num_cols needs to be so that you don't exceed the maximum subscript value with RVCC[&ind,∼]=...
SAS Super FREQ
Posts: 3,232

Re: matrix allocation

Allocation won't solve the problem because you have a call to PROC IML inside a macro loop. Every time PROC IML is called, the previous RVCC matrix is wiped out when PROC IML initializes.

The best way to arrange the code is to use an IML loop instead of a macro loop. If for some reason you can't do that (I suspect the "..." ellipses might involve other PROCs or DATA steps?) then you'll need to save the result of each iteration in a data set. You can say:

%macro cal();
%do ind=1 %to 5;
%do sim=1 %to 3;
proc iml;
ind = &ind;
sim = ∼ ;
RVCC = ind+sim;

if ind=1 & sim= 1 then
create RVCC var {ind sim RVCC};
else
edit RVCC var {ind sim RVCC};
append;
close RVCC;

quit;
%end;
%end;
%mend;

%cal();
proc print data=RVCC; run;

Or at the end you can reenter IML and use the SHAPE function to reshape your data:


proc iml;
use RVCC;
read all var {"RVCC"} into v;
close RVCC;
m = shape(v, 5, 3); /* reshape into 5x3 matrix */
print m;
Post a Question
Discussion Stats
  • 2 replies
  • 209 views
  • 0 likes
  • 3 in conversation