BookmarkSubscribeRSS Feed
tomtom09
Fluorite | Level 6

Can anybody help me that how I can calculate the correlation between two variables within each group in Proc Sql? Is there any such function just as sum or mean?

I want to do something like

proc sql;

select groupvar, corr(var1, var2) from table

group by groupvar;

quit;

 

I need the correlation variable to be one column in the final aggregated dataset. 

 

Thanks a lot! 

 

7 REPLIES 7
Reeza
Super User

You can't use SQL, you need PROC CORR. 

 

If there's something in PROC CORR that doesn't meet your requirements specify it and we can help you customize your output. 

tomtom09
Fluorite | Level 6

Thanks! I sorted the data and name it as outsort and I used the below code to create the correlation table. However there are a lot of informaiton there. Do you guys know what's the option that restrict the results only to correlation? Thanks again!

 

PROC CORR DATA= outsort OUTP=outcorr noprint;

VAR var1 var2;

BY groupvar;

RUN;

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

I am not familiar with the proc corr that much, so would refer to the docs.  However one thing you can do (on all procedures) is to check out what its doing behind the scenes.  Put:

ods trace on;

Before your proc corr call, then in the log you will see all the objects that procedure creates.  (trace off turns it off again).  You can then select parts of the output by:

ods output <objectname>=<datasetname>;

Where object name comes from the log output of the objects name, and datasetname is the dataset you want to store the results in.

In this way you can pull out bits of the output from a procedure.

tomtom09
Fluorite | Level 6
Thanks!
Reeza
Super User

Noprint doesn't suppress the output? 

 

Or are you referring to the outcorr dataset?

tomtom09
Fluorite | Level 6

Hi I am talking about the outcorr dataset. Thank you!

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Correlation is a more advanced stats procedure, and not in the normal list of simple aggregate functions associated with SQL.  Therefore I would say no.  Create your correlations using IML or proc corr, then merge any results you need in the orginal data back on.

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 860 views
  • 0 likes
  • 3 in conversation