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

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
PeterClemmensen
Tourmaline | Level 20

@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;

 

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

@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
Fluorite | Level 6
Great, thank you for your help!
Rick_SAS
SAS Super FREQ

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;
Antoine44
Fluorite | Level 6
That works too, thanks for your help! 🙂

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Multiple Linear Regression in SAS

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.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 4 replies
  • 1075 views
  • 1 like
  • 3 in conversation