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
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;
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;
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
YEAR | BIOBRC | BioRB | YBRC | YiRB | HBRC | HRB | NBRC | NRB | CBRC |
1936 | 41.1 | 50.75 | 1787.81 | 2079.78 | 102.665 | 153.503 | 0.92874 | 0.93646 | 16.945 |
1938 | 34.2 | 44.7 | 796.66 | 721.53 | 93.833 | 138.833 | 0.80946 | 0.98469 | 13.9577 |
1939 | 59.7 | 40.4 | 1502.33 | 1362.49 | 102.915 | 148.583 | 1.25295 | 0.65253 | 24.6763 |
1942 | 40 | 57.7 | 1999.36 | 1947.99 | 112.833 | 163.085 | 0.88834 | 1.07001 | 16.7522 |
1947 | 32.55 | 44.2 | 2141.84 | 1872.04 | 101.668 | 135.083 | 0.69818 | 0.87174 | 13.5091 |
1948 | 42.85 | 47.15 | 2059.13 | 2085.76 | 100.998 | 155.335 | 0.98311 | 0.97765 | 17.6518 |
1951 | 43.9 | 47.45 | 2544.66 | 2093.58 | 99.833 | 139.415 | 1.01127 | 0.9891 | 18.2257 |
1955 | 44.8 | 51.2 | 2082.27 | 1614.91 | 113 | 140.415 | 1.07822 | 1.04774 | 18.3954 |
1966 | 32.4 | 36.2 | 2431.77 | 1983.6 | 114 | 157 | 0.8777 | 0.83513 | 13.4189 |
1969 | 48.05 | 42.05 | 2464.15 | 2611.9 | 106.083 | 132.165 | 1.13481 | 0.74554 | 19.8831 |
Hussien
This is very helpful.
How can I increase the font size of the texts on the x- and y-axis?
Thanks
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?
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.