BookmarkSubscribeRSS Feed
Stephane
Quartz | Level 8
Hi

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

Is it possible ?
10 REPLIES 10
Paige
Quartz | Level 8
"bring back"?? I don't understand what you are asking for.
Rick_SAS
SAS Super FREQ
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;
Stephane
Quartz | Level 8
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
Rick_SAS
SAS Super FREQ
> 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
Stephane
Quartz | Level 8
> 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
Rick_SAS
SAS Super FREQ
Of course. Sorry. The variances are in v; the covariances are in low.
Stephane
Quartz | Level 8
Thank you very much Rick.
MC1985
Obsidian | Level 7

Hi rick,

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

thank you bery much

MC

Martino Crippa
Rick_SAS
SAS Super FREQ

Please start a new thread in the Base SAS Programming community rather than re-opening a SAS/IML thread from 2010.  You can solve this problem in the DATA step by using arrays and the _N_ automatic variable.

MC1985
Obsidian | Level 7

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

Martino Crippa

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 10 replies
  • 2761 views
  • 0 likes
  • 4 in conversation