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 05-31-2022 03:57 PM
(665 views)

I have a square 10X10 data matrix that is a correlation matrix with values of 1 along the diagonal. I am trying to find the median correlation value in this data matrix, but I cannot seem to find a function that will allow me to only extract the median from the bottom half of the matrix. Is there such a function in SAS IML? While the min() function seems to find the minimum, the median() function is giving the median value of each column. Is there a way to get the median value of the values in the lower half of a symmetrical matrix?

E.g., in this case the median correlation in the R correlation matrix is (.44 +.56)/ 2 = 0.5:

R = {1 .56 .44 .09,

.56 1 .32 .88,

.44 .32 1 .77,

.09 .88 .77 1}

3 REPLIES 3

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

Yes, you can do what you are asking, but it isn't clear to me that it is the correct computation.

First, to answer your question. You can use the ROW and COL functions to find the subscript of the elements in the upper-triangular portion.... Set those elements to a missing value, then use the MEDIAN function to get the median of each column:

```
proc iml;
R = {1 .56 .44 .09,
.56 1 .32 .88,
.44 .32 1 .77,
.09 .88 .77 1};
R0 = R; /* save a copy in case we need it */
UpperIdx = loc(row(R) < col(R));
R[UpperIdx] = .;
print R;
medLower = median(R);
print medLower;
```

However, I don't think this is right. You are trying to find the median value of the correlation between the i_th variable and the other variables. So it seems like you would want all of the correlation coefficients except the diagonal. For example, the correlations with the third variable are 0.44, 0.32, and 0.88. It seems like those are the values you would want to use, not just 0.88.

If you want to consider this alternative, you can set the diagonal element of the matrix to a missing value and compute the median of the remaining elements in each column:

```
R = R0;
/* https://blogs.sas.com/content/iml/2013/10/21/assign-the-diagonal-elements-of-a-matrix.html */
diagIdx = loc(row(R) = col(R));
R[diagIdx] = .;
print R;
med = median(R);
print med;
```

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

Or you want pool "the bottom half of the matrix." together and calculated Median ?

proc iml; R = {1 .56 .44 .09, .56 1 .32 .88, .44 .32 1 .77, .09 .88 .77 1}; R0 = R; /* save a copy in case we need it */ UpperIdx = loc(row(R) < col(R)); R[UpperIdx] = .; print R; medLower = median(R); print medLower; R = R0; /* https://blogs.sas.com/content/iml/2013/10/21/assign-the-diagonal-elements-of-a-matrix.html */ diagIdx = loc(row(R) = col(R)); R[diagIdx] = .; print R; med = median(R); print med; col_med=median(colvec(r)); print col_med; quit;

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

**SAS Innovate 2025** is scheduled for May 6-9 in Orlando, FL. Sign up to be **first to learn** about the agenda and registration!

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.