## Basics of proc iml

Does anyone know what is wrong with this code:

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

xt=t(x);
print transpose;

Using this data, I am expected to find x', x'x, x'x inverse, X’Y, b = (X’X)-1X’Y, e = Y-Xb, etc. Does anyone know how to best do this?

 X Y 123456 0.83  7.4814.4512.7020.4625.73

4 REPLIES 4

## Re: Basics of proc iml

Have a look at the results when you run this:

``````proc iml;
/* transpose a matrix */
s = {1 2 3, 4 5 6, 7 8 9, 10 11 12}; /* 4 x 3 matrix */
transpose = t(s); /* 3 x 4 matrix */
print transpose;
run;

proc iml;
x = {1, 2, 3, 4, 5, 6};
xt = t(x);
print xt;
run;``````

## Re: Basics of proc iml

@EricB40 wrote:

Using this data, I am expected to find x', x'x, x'x inverse, X’Y, b = (X’X)-1X’Y, e = Y-Xb, etc. Does anyone know how to best do this?

You turn these formulas into the appropriate PROC IML code. It should be very straighforward, the actual math formulas have a very simple and easy-to-understand representation in code

Example:

x'x in IML code is

``t(x)*x``

You need to do this for the rest of the functions you need. Show us what you have tried.

--
Paige Miller  Ksharp
Super User

## Re: Basics of proc iml

Why not post it at IML forum, since it is about IML question. and @Rick_SAS  is there.

``````data have;
input x y;
cards;
1  0.83
2 7.48
3 14.45
4 12.70
5 20.46
6 25.73
;

proc iml;
use have;
close;

x=j(nrow(x),1,1)||x;
xt=t(x);
xtx=xt*x;
xtx_inv=inv(xtx);
xty=xt*y;
b=solve(xtx,xty);
bb=inv(t(x)*x)*t(x)*y; /*another way to get BETA*/
e=y-x*b;

print x,xt,xtx,xtx_inv,xty,b ,bb ,e;
quit;``````  Ksharp
Super User

From The DO Loop