BookmarkSubscribeRSS Feed
huhuhu
Obsidian | Level 7

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
 xy
x10.5153
  
y0.51531
  

 

Any suggestions would be appreciated!

7 REPLIES 7
rayIII
SAS Employee

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

huhuhu
Obsidian | Level 7

Thank you for your help

Ksharp
Super User
Want IML code ?



proc iml;
use sashelp.iris nobs nobs;
read all var{species petallength sepallength};
close;

start_end=t( loc(t(species) ^= ' '||remove(species,nobs)) )||
          t( loc(t(species) ^= remove(species,1)||' ') );

group=species[start_end[,1]];
corr=j(nrow(group),1,.);
do i=1 to nrow(group);
 idx=start_end[i,1]:start_end[i,2]; 
 temp=petallength[idx]|| sepallength[idx];
 corr[i]=corr(temp)[2];
end;
create want from corr[r=group c={corr}];
append from corr[r=group];
close;
quit;

proc print noobs;run;



OUTPUT:

group	CORR
Setosa	0.26718
Versicolor	0.75405
Virginica	0.86422

Rick_SAS
SAS Super FREQ

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;

 

 

 

Ksharp
Super User
Rick,
It could be simple as :


proc corr data = sashelp.iris noprint 
          out=outCorr(where=(_TYPE_="CORR"));
by species;
var petallength ;
with sepallength;
run; 
proc print noobs;run;

huhuhu
Obsidian | Level 7

Thank you very much! That's even simpler!

huhuhu
Obsidian | Level 7

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

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!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 5993 views
  • 4 likes
  • 4 in conversation