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

Hi Jeff,

 

The code is working properly and the matrix looks good. Thank you so much for your help and time. However, I noticed when using big set of data the , the map is so busy. Is there possibility to use  * for P <0.05 and  and ** for P <0.001 ,  *** for P <0.0001, and nothing when P > 0.05? That might make it more  clear.

 

Kind Regards

 

 

 

 

JeffMeyers
Barite | Level 11

I did this by making a format for the p-values and applying it.  I added an ENTRYFOOTNOTE for a footnote clarifying the symbols, and you can change the text to be what you'd like.

 

data work.Almtarfi_2;
input Cult Biomass_BRC Biomass_RB yield_BRC Yield_RB Height_BRC Heigh_RB;
CARDS;
1930 62.125 75.255 2563.16 2196.06 123.075 142.8
1933 60.62 77.91 2396.49 1965.79 106.233 124.325
1935 51.275 77.2 2317.14 2490.56 114.75 137.25
1940 63.31 73.025 2738.19 2888.38 142.85 169.5
;
run;
proc format;

    value pvaldots
        0-<0.0001 = '***'
        0.0001-<0.001 = '**'
        0.001-<0.05 = '*'
        0.05-high =' '
        other = ' ';
run;
        * for P <0.05 and  and ** for P <0.001 ,  *** for P <0.0001, and nothing when P > 0.05;
/*Code below by Chris Hemedinger, found on the SAS blogs*/
ods trace off;
/* Prepare the correlations coeff matrix: Pearson's r method */
%macro prepCorrData(in=,out=);
/* Run corr matrix for input data, all numeric vars */
ods select none; ods noresults;
ods output PearsonCorr=blah;
proc corr data=&in. pearson 
outp=work._tmpCorr
vardef=df
;
run;
ods results;
ods select all;
proc sql noprint;
    select variable into :vlist separated by '|' from blah;
quit;
/* prep data for heat map */
data &out.;
keep x y r p;
format p pvaldots.;
set blah (rename=(variable=_name_));
*set work._tmpCorr(where=(_TYPE_="CORR"));
array v{*} %do i = 1 %to %sysfunc(countw(%superq(vlist),|)); %scan(%superq(vlist),&i,|) %end;;
array pval{*} %do i = 1 %to %sysfunc(countw(%superq(vlist),|)); p%scan(%superq(vlist),&i,|) %end;;
x = _NAME_;
do i = dim(v) to 1 by -1;
y = vname(v(i));
r = v(i);
p = pval(i);

/* creates a lower triangular matrix */
if (i<_n_) then do;
r=.;p=.;
end;
output;
end;
run;

proc datasets lib=work nolist nowarn;
delete _tmpcorr;
quit;
%mend;
/* Create a heat map implementation of a correlation matrix */
ods path work.mystore(update) sashelp.tmplmst(read);

proc template;
define statgraph corrHeatmap;
dynamic _Title;
begingraph;
entrytitle _Title;
entryfootnote halign=left '*: P <0.05;**: P <0.001;***: P <0.0001;Blank: P > 0.05' / textattrs=(size=10pt);
rangeattrmap name='map';
/* select a series of colors that represent a "diverging" */
/* range of values: stronger on the ends, weaker in middle */
/* Get ideas from http://colorbrewer.org */
range -1 - 1 / rangecolormodel=(cxD8B365 cxF5F5F5 cx5AB4AC);
endrangeattrmap;
rangeattrvar var=r attrvar=r attrmap='map';
layout overlay /
xaxisopts=(display=(line ticks tickvalues))
yaxisopts=(display=(line ticks tickvalues));
heatmapparm x = x y = y colorresponse = r /
xbinaxis=false ybinaxis=false
name = "heatmap" display=all;
textplot x=x y=y text=p / textattrs=(size=14pt);
continuouslegend "heatmap" /
orient = vertical location = outside title="Pearson Correlation";
endlayout;
endgraph;
end;
run;

/* Build the graphs */
ods graphics /height=600 width=800 imagemap;
%prepCorrData(in=work.Almtarfi_2,out=work.Almtarfi_r);
proc sgrender data=work.Almtarfi_r template=corrHeatmap;
dynamic _title="Correlation matrix for all variables";
run;

SGRender.png

Huss
Fluorite | Level 6

Hi Jeff,

 

Thanks a lot for again for you  help . When adding the proc corr numeric matrix code , I found that it is only adding star instead of  two for significant . It is considering for example adding two star only when P- value <0.001 and one if it is equal to 0.001 which is not appropriate. If you look at the p-value in numeric  matrix and the stars in the heat map , you see it is not the same significant., for example Height- RB vs Height- BRC. I tested with other data and it is the same. Can you please , help with fixing the ENTRYFOOTNOTE , please

 

 

 


data work.Almtarfi_2;
input Cult Biomass_BRC Biomass_RB yield_BRC Yield_RB Height_BRC Heigh_RB;
CARDS;
1930 62.125 75.255 2563.16 2196.06 123.075 142.8
1933 60.62 77.91 2396.49 1965.79 106.233 124.325
1935 51.275 77.2 2317.14 2490.56 114.75 137.25
1940 63.31 73.025 2738.19 2888.38 142.85 169.5
;
run;
proc format;

value pvaldots
0-<0.0001 = '***'
0.0001-<0.001 = '**'
0.001-<0.05 = '*'
0.05-high =' '
other = ' ';

run;
* for P <0.05 and and ** for P <0.001 , *** for P <0.0001, and nothing when P > 0.05;
/*Code below by Chris Hemedinger, found on the SAS blogs*/
ods trace off;
/* Prepare the correlations coeff matrix: Pearson's r method */
%macro prepCorrData(in=,out=);
/* Run corr matrix for input data, all numeric vars */
ods select none; ods noresults;
ods output PearsonCorr=blah;
proc corr data=&in. pearson
outp=work._tmpCorr
vardef=df;
run;
ods results;
ods select all;
proc sql noprint;
select variable into :vlist separated by '|' from blah;
quit;

PROC CORR DATA=work.Almtarfi_2;
VAR Cult Biomass_BRC Biomass_RB yield_BRC Yield_RB Height_BRC Heigh_RB;
WITH Cult Biomass_BRC Biomass_RB yield_BRC Yield_RB Height_BRC Heigh_RB;
RUN;
/* prep data for heat map */
data &out.;
keep x y r p;
format p pvaldots.;
set blah (rename=(variable=_name_));
*set work._tmpCorr(where=(_TYPE_="CORR"));
array v{*} %do i = 1 %to %sysfunc(countw(%superq(vlist),|)); %scan(%superq(vlist),&i,|) %end;;
array pval{*} %do i = 1 %to %sysfunc(countw(%superq(vlist),|)); p%scan(%superq(vlist),&i,|) %end;;
x = _NAME_;
do i = dim(v) to 1 by -1;
y = vname(v(i));
r = v(i);
p = pval(i);

/* creates a lower triangular matrix */
if (i<_n_) then do;
r=.;p=.;
end;
output;
end;
run;

proc datasets lib=work nolist nowarn;
delete _tmpcorr;
quit;
%mend;
/* Create a heat map implementation of a correlation matrix */
ods path work.mystore(update) sashelp.tmplmst(read);

proc template;
define statgraph corrHeatmap;
dynamic _Title;
begingraph;
entrytitle _Title;
entryfootnote halign=left '*: P <0.05;**: P <0.001;***: P <0.0001;Blank: P > 0.05' / textattrs=(size=10pt);
rangeattrmap name='map';
/* select a series of colors that represent a "diverging" */
/* range of values: stronger on the ends, weaker in middle */
/* Get ideas from http://colorbrewer.org */
range -1 - 1 / rangecolormodel=(cxD8B365 cxF5F5F5 cx5AB4AC);
endrangeattrmap;
rangeattrvar var=r attrvar=r attrmap='map';
layout overlay /
xaxisopts=(display=(line ticks tickvalues))
yaxisopts=(display=(line ticks tickvalues));
heatmapparm x = x y = y colorresponse = r /
xbinaxis=false ybinaxis=false
name = "heatmap" display=all;
textplot x=x y=y text=p / textattrs=(size=14pt);
continuouslegend "heatmap" /
orient = vertical location = outside title="Pearson Correlation";
endlayout;
endgraph;
end;
run;

/* Build the graphs */
ods graphics /height=600 width=800 imagemap;
%prepCorrData(in=work.Almtarfi_2,out=work.Almtarfi_r);
proc sgrender data=work.Almtarfi_r template=corrHeatmap;
dynamic _title="Correlation matrix for all variables";
run;

Huss
Fluorite | Level 6

Hi everyone,

I am looking for your help , please to  get A Principle Component Analysis  SAS code for the data below with output graphs including grouping and highlighting the variables in different colors. I would really appreciate it.

 

Thanks

 

YEARBIOBRC BioRBYBRCYiRBHBRCHRBNBRCNRBCBRC
193641.150.751787.812079.78102.665153.5030.928740.9364616.945
193834.244.7796.66721.5393.833138.8330.809460.9846913.9577
193959.740.41502.331362.49102.915148.5831.252950.6525324.6763
19424057.71999.361947.99112.833163.0850.888341.0700116.7522
194732.5544.22141.841872.04101.668135.0830.698180.8717413.5091
194842.8547.152059.132085.76100.998155.3350.983110.9776517.6518
195143.947.452544.662093.5899.833139.4151.011270.989118.2257
195544.851.22082.271614.91113140.4151.078221.0477418.3954
196632.436.22431.771983.61141570.87770.8351313.4189
196948.0542.052464.152611.9106.083132.1651.134810.7455419.8831

 

 

Hussien

 

 

iatoloye
Fluorite | Level 6

This is very helpful.

 

How can I increase the font size of the texts on the x- and y-axis?

 

Thanks

iatoloye
Fluorite | Level 6

This is very useful. I need help on how to increase the font size of the axes labels. I used labelattrs but it did not work.

 

Any help?

Reeza
Super User
And which version of SAS do you have?
Such as 9.4TS1M5?
Rick_SAS
SAS Super FREQ

the article "Create heat maps with PROC SGPLOT" provides an example of overlaying text on a heatmap by using the HEATMAP and TEXt statements in PROC SGPLOT. Although the example in the article is a frequency table and not a correlation matrix, the idea is the same.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 22 replies
  • 6151 views
  • 8 likes
  • 5 in conversation