🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Fluorite | Level 6

Matrix from dataset and calculations

Hi all,

Need some help regarding matrices in SAS. I've got two datasets (large ones) from which I'd like to create matrices and do some calculations. The formula I'm trying to apply is x'(X'X)^-1x. X and x would be two matrices from two datasets.

So far I've got the code below but it's incomplete and can't figure out the rest.

```proc iml;
use matrix;
read all var {mktcap cash vol price} into X;
Xtranspose = X`;
XtransX = X*X`;
print X Xtranspose XtransX;
```

Any help regarding how to calculate that will be very much appreciated 🙂

1 ACCEPTED SOLUTION

Accepted Solutions
Tourmaline | Level 20

Re: Matrix from dataset and calculations

@Antoine44 See if you can use the code below as a template. Just created some small sample data.

Also, I moved this topic to the IML Forum.

``````data x1;
input mktcap cash vol price;
datalines;
5 1 9 3
7 3 5 1
6 3 9 5
4 7 1 3
;

data x2;
input mktcap cash vol price;
datalines;
6 4 6 2
8 3 6 1
3 5 8 1
9 4 6 1
;

proc iml;
use x1;
read all var {mktcap cash vol price} into X1;
close x1;

use x2;
read all var {mktcap cash vol price} into X2;
close x2;

f = X1` * inv((X2` * X2)) * X1;

print f;
quit;``````

4 REPLIES 4
Tourmaline | Level 20

Re: Matrix from dataset and calculations

@Antoine44 See if you can use the code below as a template. Just created some small sample data.

Also, I moved this topic to the IML Forum.

``````data x1;
input mktcap cash vol price;
datalines;
5 1 9 3
7 3 5 1
6 3 9 5
4 7 1 3
;

data x2;
input mktcap cash vol price;
datalines;
6 4 6 2
8 3 6 1
3 5 8 1
9 4 6 1
;

proc iml;
use x1;
read all var {mktcap cash vol price} into X1;
close x1;

use x2;
read all var {mktcap cash vol price} into X2;
close x2;

f = X1` * inv((X2` * X2)) * X1;

print f;
quit;``````

Fluorite | Level 6

Re: Matrix from dataset and calculations

Great, thank you for your help!
SAS Super FREQ

Re: Matrix from dataset and calculations

You probably don't need to compute the inverse matrix. It will be more efficient to use

``````proc iml;
use x1;
read all var {mktcap cash vol price} into X1;
close x1;
use x2;
read all var {mktcap cash vol price} into X2;
close x2;
/*
f = X1` * inv((X2` * X2)) * X1;
print f;
*/
A = X2` * X2;
Z = solve(A, X1);
f = X1` * Z;
print f;``````
Fluorite | Level 6

Re: Matrix from dataset and calculations

That works too, thanks for your help! 🙂
From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
• 4 replies
• 780 views
• 1 like
• 3 in conversation