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 🙂
@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;
@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;
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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.