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
Super User

@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
Super User

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

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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
  • 501 views
  • 1 like
  • 3 in conversation