BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Excelsius
Obsidian | Level 7

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.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;
Spoiler

@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.

 

 


 

View solution in original post

6 REPLIES 6
Reeza
Super User

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;
Spoiler

@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.

 

 


 

Excelsius
Obsidian | Level 7

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.

Reeza
Super User
Regarding the circles, that's a different viz type, at the point, I'd almost consider a scatter plot controlling the symbols to get the shapes you want and putting the values in as text or marker char values. You'd have to determine the size/spacing which would be a bit of a guess and test procedure. For any topic you're having issues with in SAS search lexjansen.com, especially if it's topic specific. Otherwise, I pretty much rely on the docs but I also use R and Tableau for graphics 🙂
ChrisHemedinger
Community Manager

@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.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
Excelsius
Obsidian | Level 7

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.

DanH_sas
SAS Super FREQ

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-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
  • 6 replies
  • 2725 views
  • 3 likes
  • 4 in conversation