## How to create a matrix with multiplied terms (SAS IML)

Occasional Contributor
Posts: 11

# How to create a matrix with multiplied terms (SAS IML)

(SAS Iml)

I have a specific data and now I need to know how I can produce a matrix with the following formula:

1/2+sum of multiplications of row(i)-row(n). That is to say, I have a matrix with let say 2 columns and 3 rows and I need to produce a new matrix by calculating a value for each element (value in the cell=v(row,column).

column1     column2

row1     v(1,1)          v(1,2)

row2     v(2,1)          v(2,2)

row3     v(3,1)          v(3,2)

column1                                                                         column2

row1    1/2+v(1,1)+v(1,1)*v(2,1)+v(1,1)*v(2,1)*v(3,1)               1/2+v(1,2)+v(1,2)*v(2,2)+v(1,2)*v(2,2)*v(3,2)

row2    1/2+v(2,1)+v(2,1)*v(3,1)                                              1/2+v(2,2)+v(2,2)*v(3,2)

row3    1/2+v(3,1)                                                                    1/2+v(3,2)

Posts: 2,655

## Re: How to create a matrix with multiplied terms (SAS IML)

Hi Milla,

I hope you also post this in the SAS/IML and SAS/IML Studio forum.  The folks who follow that forum closely should be able to give you a good answer.

Steve Denham

Super Contributor
Posts: 339

## Re: How to create a matrix with multiplied terms (SAS IML)

I was playing real fast with IML but its been forever since I've used it and I couldn't remember any function mimicking what I eventually hard coded with additionnal loops but here's a quick glance that might help you. It achieves the desired result and should be generic enough to take any size matrix. It is likely not the most efficient approach though as well as not the simplest code to read.

x={1 4, 2 5, 3 6};
print x;

y=j(nrow(x), nrow(x), 0);
z=j(nrow(x), ncol(x), 0);

do i=1 to nrow(y);
do j=1 to ncol(y);
if j<=i then y[i,j]=1;
else y[i,j]=0;
end;
end;

do i=1 to ncol(x);
yy=y#x[,i]`;
do ii=1 to nrow(yy);
do jj=1 to ncol(yy);
if yy[ii, jj]=0 then yy[ii,jj]=1;
end;
end;
print yy;
z[, i]=1/2+yy[,#];
end;

print z;

Hope it will be useful nonetheless

Vince

Occasional Contributor
Posts: 11

## Re: How to create a matrix with multiplied terms (SAS IML)

Hi Vince, I don't think it's actually working As you can see, the desired result matrix should look like the lower one I posted, however e.g. the element in (3,1) of the produced matrix  seems to differ from 1/2+v(3,1) and so fort. So basically what you did was kind of the opposite, so you multiplied each element with the previous one(s) not the following ones and you didn't sum them at all.

So e.g. the element in (1,1) of the produced matrix should look like this, and I'm adding more terms to show it more accurately (matrix is now 5x2):

1/2+v(1,1)+v(1,1)*v(2,1)+v(1,1)*v(2,1)*v(3,1)+v(1,1)*v(2,1)*v(3,1)*v(4,1)+v(1,1)*v(2,1)*v(3,1)*v(4,1)*v(5,1)

and element in (5,1)

1/2+v(5,1)

So I probably wasn't being clear enough... It's hard to explain it other than showing the desired production matrix as I did in the first place.

Discussion stats
• 3 replies
• 287 views
• 0 likes
• 3 in conversation