turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- How to create a matrix with multiplied terms (SAS ...

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

12-27-2013 01:39 AM

(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)

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Milla

12-30-2013 11:29 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Milla

12-30-2013 01:25 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Vince28_Statcan

12-31-2013 01:57 AM

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.