Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 01-20-2021 12:15 AM
(500 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

@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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Great, thank you for your help!

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

That works too, thanks for your help! 🙂

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. **

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.