I'm not an avid SAS user and have been stuck at this step: I have a working heatmap
ods output PearsonCorr=Corr_P;
PROC CORR DATA=sashelp.cars;
VAR MSRP Horsepower MPG_City MPG_Highway Weight;
RUN;
proc sort data=Corr_P;
by VARIABLE;
run;
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;
data CorrLong;
merge CorrLong1 CorrLong2(drop=PvalueID _LABEL_));
by Variable;
drop _name_;
LABEL _NAME_=' '
run;
proc sort data=CorrLong;
by VARIABLE CorrelationID;
run;
proc sgplot data=CorrLong;
heatmap x=Variable y=CorrelationID / colorresponse=Correlation discretex discretey x2axis;
yaxis reverse display=(nolabel);
x2axis display=(nolabel);
run;
Everything works fine above, but as soon as I add a text overlay, I get an x axis label that I'm unable to get rid of:
proc sgplot data=CorrLong;
heatmap x=Variable y=CorrelationID / colorresponse=Correlation 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; /*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);
run;
I've already tried noautolegend + gradlegend, keylegend, colormodel with various combinations and cannot resolve this stubborn issue. I know it's a keylegend item because I can move it around with that option, but keylegen exclude option did not get rid of it. My goal is to keep the gradient legend on the right side. I'm sure someone can tell me what's the obvious code that I'm missing.
Thank you.
Note: this was edited to make sure the gradient legend is included.
NOAUTOLEGEND + GRADLEGEND works for me. What version of SAS are you using? You can check your status with the following:
proc product_status;run;
This is the working code, your sample didn't run so I'm including the full code after I modified it.
ods output PearsonCorr=Corr_P;
PROC CORR DATA=sashelp.cars;
VAR MSRP Horsepower MPG_City MPG_Highway Weight;
RUN;
proc sort data=Corr_P;
by VARIABLE;
run;
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;
data CorrLong;
merge CorrLong1 CorrLong2(drop=PvalueID );
by Variable;
LABEL CorrelationID="Correlations"; run;
proc sort data=CorrLong;
by VARIABLE CorrelationID;
run;
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;
@Excelsius wrote:
I'm not an avid SAS user and have been stuck at this step: I have a working heatmap
ods output PearsonCorr=Corr_P; PROC CORR DATA=sashelp.cars; VAR MSRP Horsepower MPG_City MPG_Highway Weight; RUN; proc sort data=Corr_P; by VARIABLE; run; 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; data CorrLong; merge CorrLong1 CorrLong2(drop=PvalueID _LABEL_)); by Variable; drop _name_; LABEL _NAME_=' ' run;
proc sort data=CorrLong;
by VARIABLE CorrelationID;
run;
proc sgplot data=CorrLong;
heatmap x=Variable y=CorrelationID / colorresponse=Correlation discretex discretey x2axis;
yaxis reverse display=(nolabel);
x2axis display=(nolabel);
run;
Everything works fine above, but as soon as I add a text overlay, I get an x axis label that I'm unable to get rid of:
proc sgplot data=CorrLong; heatmap x=Variable y=CorrelationID / colorresponse=Correlation 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; /*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); run;
I've already tried noautolegend + gradlegend, keylegend, colormodel with various combinations and cannot resolve this stubborn issue. I know it's a keylegend item because I can move it around with that option, but keylegen exclude option did not get rid of it. My goal is to keep the gradient legend on the right side. I'm sure someone can tell me what's the obvious code that I'm missing.
Thank you.
Note: this was edited to make sure the gradient legend is included.
NOAUTOLEGEND + GRADLEGEND works for me. What version of SAS are you using? You can check your status with the following:
proc product_status;run;
This is the working code, your sample didn't run so I'm including the full code after I modified it.
ods output PearsonCorr=Corr_P;
PROC CORR DATA=sashelp.cars;
VAR MSRP Horsepower MPG_City MPG_Highway Weight;
RUN;
proc sort data=Corr_P;
by VARIABLE;
run;
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;
data CorrLong;
merge CorrLong1 CorrLong2(drop=PvalueID );
by Variable;
LABEL CorrelationID="Correlations"; run;
proc sort data=CorrLong;
by VARIABLE CorrelationID;
run;
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;
@Excelsius wrote:
I'm not an avid SAS user and have been stuck at this step: I have a working heatmap
ods output PearsonCorr=Corr_P; PROC CORR DATA=sashelp.cars; VAR MSRP Horsepower MPG_City MPG_Highway Weight; RUN; proc sort data=Corr_P; by VARIABLE; run; 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; data CorrLong; merge CorrLong1 CorrLong2(drop=PvalueID _LABEL_)); by Variable; drop _name_; LABEL _NAME_=' ' run;
proc sort data=CorrLong;
by VARIABLE CorrelationID;
run;
proc sgplot data=CorrLong;
heatmap x=Variable y=CorrelationID / colorresponse=Correlation discretex discretey x2axis;
yaxis reverse display=(nolabel);
x2axis display=(nolabel);
run;
Everything works fine above, but as soon as I add a text overlay, I get an x axis label that I'm unable to get rid of:
proc sgplot data=CorrLong; heatmap x=Variable y=CorrelationID / colorresponse=Correlation 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; /*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); run;
I've already tried noautolegend + gradlegend, keylegend, colormodel with various combinations and cannot resolve this stubborn issue. I know it's a keylegend item because I can move it around with that option, but keylegen exclude option did not get rid of it. My goal is to keep the gradient legend on the right side. I'm sure someone can tell me what's the obvious code that I'm missing.
Thank you.
Reeza, thank you very much. That works (9.4 M5 14.3). The reason it didn't work for me was because I was using gradlegend 'heatmap'; It didn't occur to me to just use gradlegend alone. This is why SAS syntax can be so frustrating sometimes. I was able to make this same graph in R within 20 min despite that I know R less than SAS!
Honestly, I'm still not sure what's the difference between gradlegend, keylegend, legenditem, etc. Some of this stuff is easiest to learn with examples, but I didn't come across that many. If you have any suggestions for good sources, other than the SAS manual online, let me know. I've already gone through the Little SAS book.
I'm also curious if there is a way to make half of the correlation matrix into a different shape, such as circles based on the correlation value (without messing with SAS/IML).
Thanks again.
@Jay54 and @Rick_SAS often write blog examples that demonstrate SGPLOT features like this. Here's a gradlegend example from Rick. And another from Sanjay is here.
Thanks. I'll read these blogs over time. I have already read some of Rick's blogs.
Question: the gradlegend in the example above has a scale that only includes -0.5 and +0.5. Is it possible to customize this scale to display numbers with 0.2 increments (-0.4, -0.2, 0, 0.2, etc)? The extractscale option doesn't seem to do anything.
For legends (keylegend, gradlegend, etc), you should refer to the plot's NAME value -- not the statement name. In Reeza's example, the heatmap has the name of "nope1". So, in this case, you would say:
gradlegend "nope1";
Hope this helps!
Dan
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.