Turn on suggestions

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

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- How to create a table of correlations for many subgroups?

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 08-29-2016 11:37 AM
(6549 views)

Dear all SAS experts,

I'd like to have a table of correlation between two variables (for example, x and y) by many groups. My dream matrix would like this:

GroupNumber Correlation between x and y

1 0.5153

2 0.6

3 0.3

4 0.6

5 0.2

I can use BY statement in PROC CORR procedures, but I would get many seperate tables for each subgroup in the BY variable like below (an example result for groupNumber 1), and it's time consuming to build my dream table by hand since it comprosed of more than 100 sugroups each year.

Pearson Correlation Coefficients, N = 8996452 | ||

Prob > |r| under H0: Rho=0 | ||

x | y | |

x | 1 | 0.5153 |

y | 0.5153 | 1 |

Any suggestions would be appreciated!

7 REPLIES 7

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

This will give you a table (corr) of all of the coefficients. You'll just need to eliminate the redundant ones.

```
proc corr data = sashelp.iris;
var petallength sepallength;
by species;
ods output pearsonCorr = Corr;
run;
```

hope this helps.

Ray

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

Thank you for your help

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

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

You can use the NOPRINT and OUT= options to suppress the tables and create a SAS data set that contains the correlations. Then use a DATA step or a WHERE clause to subset the output statistics:

```
proc corr data = sashelp.iris noprint
out=outCorr(where=(_TYPE_="CORR"));
by species;
var petallength sepallength;
run;
proc print data=outCorr label;
where _NAME_="PetalLength"; /* var1 */
var Species /* BY var */
SepalLength; /* var2 */
label SepalLength="Corr(PetalLength, SepalLength)";
run;
```

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

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

Thank you very much! That's even simpler!

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

Thank you, that's the dream output I like to have!

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 16. Read more here about **why** you should contribute and **what is in it** for you!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.