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

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

Posted 02-20-2010 12:55 PM
(3404 views)

Hi

I'd like to bring back the lower or upper triangular matrix with IML.

Is it possible ?

I'd like to bring back the lower or upper triangular matrix with IML.

Is it possible ?

10 REPLIES 10

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

"bring back"?? I don't understand what you are asking for.

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

It's always a good idea to supply an example.

Are you saying that you have a matrix

a = {1 2 3, 4 5 6, 7 8 9};

and you want the lower triangular values?

If so, do you want them in a vector or in a matrix?

To get the values in a vector, you can use

lower = symsqr(a);

upper = symsqr(a`);

If you need the lower triangular matrix with zeros above the diagonal, you can use:

n = nrow(a);

p = ncol(a);

low = j(n, p, 0);

do i = 1 to n;

cols = 1:i; /* or cols=i:p; */

low[i, cols] = a[i, cols];

end;

Are you saying that you have a matrix

a = {1 2 3, 4 5 6, 7 8 9};

and you want the lower triangular values?

If so, do you want them in a vector or in a matrix?

To get the values in a vector, you can use

lower = symsqr(a);

upper = symsqr(a`);

If you need the lower triangular matrix with zeros above the diagonal, you can use:

n = nrow(a);

p = ncol(a);

low = j(n, p, 0);

do i = 1 to n;

cols = 1:i; /* or cols=i:p; */

low[i, cols] = a[i, cols];

end;

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

oops sorry it's true that my explanation is light and I'm newbie in IML.

using your (excellent) proposition :

proc iml;

a={

-3 2 4,

2 -2 3,

4 3 -4};

r={"A" "B" "C"};

c={"A" "B" "C"};

v=vecdiag(a);

lower = symsqr(a);

upper = symsqr(a`);

n = nrow(a);

p = ncol(a);

low = j(n, p, .);

do i = 1 to n;

cols = 1:i; /* or cols=i:p; */

low[i, cols] = a[i, cols];

end;

print a[rowname=r colname=c];

print low[rowname=r colname=c];

quit;

You see that the "a" matrix is a variance/covariance matrix and I want the covariance values. So your low matrix is fine except that I would like the low matrix but with missing values on the diagonal.

Otherwise Rick, could you show me how to produce the mean, min, max, ... of the covariance part of the matrix for A, B and C in IML ? Message was edited by: Stephane

using your (excellent) proposition :

proc iml;

a={

-3 2 4,

2 -2 3,

4 3 -4};

r={"A" "B" "C"};

c={"A" "B" "C"};

v=vecdiag(a);

lower = symsqr(a);

upper = symsqr(a`);

n = nrow(a);

p = ncol(a);

low = j(n, p, .);

do i = 1 to n;

cols = 1:i; /* or cols=i:p; */

low[i, cols] = a[i, cols];

end;

print a[rowname=r colname=c];

print low[rowname=r colname=c];

quit;

You see that the "a" matrix is a variance/covariance matrix and I want the covariance values. So your low matrix is fine except that I would like the low matrix but with missing values on the diagonal.

Otherwise Rick, could you show me how to produce the mean, min, max, ... of the covariance part of the matrix for A, B and C in IML ? Message was edited by: Stephane

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

> I want the covariance values.

They are in v.

> I would like the low matrix but with missing values on the diagonal.

low = j(n, p, .);

do i = 2 to n;

cols = 1:i-1;

low[i, cols] = a[i, cols];

end;

> show me how to produce the mean, min, max, ... of the covariance part

> of the matrix for A, B and C in IML ?

For the matrix a, you can compute the mean (or max or min...) of three different quantities: the total mean, the rows means, or the column means.

For the totals:

mean = a[:]; /* ":" is mean operator */

min = min(a);

max = max(a);

print mean min max;

For the column means:

ColMean = a[:,]; /* mean of each column (apply operation on rows)*/

ColMin = a[><,]; /* min of each column */

ColMax = a[<>,]; /* max of each column */

print ColMean, ColMin, ColMax;

These subscript reduction operators take some getting used to, but are great for computing summary statistics without writing any loops. They are documented in the "Working with Matrices" chapter of the SAS/IML User's Guide:

http://support.sas.com/documentation/cdl/en/imlug/59656/HTML/default/workmatrix_sect14.htm

They are in v.

> I would like the low matrix but with missing values on the diagonal.

low = j(n, p, .);

do i = 2 to n;

cols = 1:i-1;

low[i, cols] = a[i, cols];

end;

> show me how to produce the mean, min, max, ... of the covariance part

> of the matrix for A, B and C in IML ?

For the matrix a, you can compute the mean (or max or min...) of three different quantities: the total mean, the rows means, or the column means.

For the totals:

mean = a[:]; /* ":" is mean operator */

min = min(a);

max = max(a);

print mean min max;

For the column means:

ColMean = a[:,]; /* mean of each column (apply operation on rows)*/

ColMin = a[><,]; /* min of each column */

ColMax = a[<>,]; /* max of each column */

print ColMean, ColMin, ColMax;

These subscript reduction operators take some getting used to, but are great for computing summary statistics without writing any loops. They are documented in the "Working with Matrices" chapter of the SAS/IML User's Guide:

http://support.sas.com/documentation/cdl/en/imlug/59656/HTML/default/workmatrix_sect14.htm

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

> I want the covariance values.

They are in v.

=> No it's the variance and this is why I want to the rest.

for your explanations for the statistics, it's fine. Message was edited by: Stephane

They are in v.

=> No it's the variance and this is why I want to the rest.

for your explanations for the statistics, it's fine. Message was edited by: Stephane

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

Of course. Sorry. The variances are in v; the covariances are in low.

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

Thank you very much Rick.

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

Hi rick,

did you have any suggests to do the same thing but without PROC IML?

thank you bery much

MC

Martino Crippa

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

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

OK, thanks. Here we are: New Topic on SAS BASE

Martino Crippa

Are you ready for the spotlight? We're accepting content ideas for **SAS Innovate 2025** to be held May 6-9 in Orlando, FL. The call is **open **until September 25. Read more here about **why** you should contribute and **what is in it** for you!

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.