SAS - correlation matrix and heat map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*This program is based on the question here: | |
https://communities.sas.com/t5/SAS-GRAPH-and-ODS-Graphics/Removing-one-of-the-legends-from-a-heatmap/m-p/456131 | |
The purpose is to create a correlation heatmap from a correlation matrix. | |
Author: F. Khurshed | |
Date: 2018-04-20 | |
*/ | |
*calculate correlation matrix for the data; | |
ods output PearsonCorr=Corr_P; | |
PROC CORR DATA=sashelp.cars; | |
VAR MSRP Horsepower MPG_City MPG_Highway Weight; | |
RUN; | |
*sort for transposes; | |
proc sort data=Corr_P; | |
by VARIABLE; | |
run; | |
*restructure data so that it's in a long format for graphing; | |
*need to transpose the correlation and p-values separately; | |
proc transpose data=Corr_P out=CorrLong1(rename=(COL1=Correlation)) | |
name=CorrelationID; | |
var MSRP Horsepower MPG_City MPG_Highway Weight; | |
by Variable; | |
run; | |
proc transpose data=Corr_P out=CorrLong2(rename=(COL1=p_value)) name=PvalueID; | |
var PMSRP PHorsepower PMPG_City PMPG_Highway PWeight; | |
by Variable; | |
run; | |
*merge data sets to get p-value and correlation in right places; | |
data CorrLong; | |
merge CorrLong1 CorrLong2(drop=PvalueID ); | |
by Variable; | |
LABEL CorrelationID="Correlations"; run; | |
*sort for graphing; | |
proc sort data=CorrLong; | |
by VARIABLE CorrelationID; | |
run; | |
*create a heat map graph with P-Values in the squares; | |
proc sgplot data=CorrLong noautolegend; | |
heatmap x=Variable y=CorrelationID / colorresponse=Correlation name="nope1" discretex discretey x2axis colormodel=ThreeColorRamp; *Colorresponse allows discrete squares for each correlation. x2axis bring the label to the top; | |
text x=Variable y=CorrelationID text=p_value / textattrs=(size=10pt) x2axis name='nope2'; /*To overlay significance, create a variable that contans that info and set text=VARIABLE */ | |
label correlation='Pearson Correlation'; | |
yaxis reverse display=(nolabel); | |
x2axis display=(nolabel); | |
gradlegend; | |
run; | |
*Another variabion from Warren Kuhfield; | |
*source https://communities.sas.com/t5/Statistical-Procedures/Filtering-Correlation-Matrix/m-p/645565; | |
proc format; | |
value CorrSignif -0.0-<0.2 = "red" | |
0.2-<0.4, -0.4-<-0.2 = "orange" | |
0.4-<0.6, -0.6-<-0.4 = "yellow" | |
0.6-<0.8, -0.8-<-0.6 = "lightgreen" | |
0.8-<1.0, -1.0-<-0.8= "forestgreen" | |
1, -1 = "White"; | |
run; | |
proc corr data=sashelp.cars outp=CorrOut (where=(_type_='CORR')) noprint; | |
run; | |
proc print data=CorrOut (drop=_type_ rename=(_name_=Variable)) | |
style(column)={backgroundcolor= CorrSignif.} noobs; | |
run; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment