/* Program to accompany "Create heat maps with PROC SGPLOT," Rick Wicklin, published 17AUG2015, The DO Loop blog, http://blogs.sas.com/content/iml/2015/08/17/heat-maps-sgplot.html ? */ /* data tabulation technique from "How to order categories in a two-way table with PROC FREQ" Rick Wicklin, published 28Oct2013 http://blogs.sas.com/content/iml/2013/10/28/how-to-order-categories-in-a-two-way-table-with-proc-freq.html */ proc format; value WtFmt 1 = 'Underweight' 2 = 'Normal' 3 = 'Overweight'; value SmFmt 1 = 'Non-smoker' 2 = 'Light (1-5)' 3 = 'Moderate (6-15)' 4 = 'Heavy (16-25)' 5 = 'Very Heavy (> 25)'; run; data Heart / view=Heart; format Smoking_Cat SmFmt. Weight_Cat WtFmt.; set sashelp.heart; select (Weight_Status); when ('Underweight') Weight_Cat=1; when ('Normal') Weight_Cat=2; when ('Overweight') Weight_Cat=3; when (' ') Weight_Cat=.; end; select (Smoking_Status); when ('Non-smoker') Smoking_Cat=1; when ('Light (1-5)') Smoking_Cat=2; when ('Moderate (6-15)') Smoking_Cat=3; when ('Heavy (16-25)') Smoking_Cat=4; when ('Very Heavy (> 25)') Smoking_Cat=5; when (' ') Smoking_Cat=.; end; run; proc freq data=heart; tables Smoking_Cat*Weight_Cat / norow nocol nopct sparse out=FreqOut(where=(percent^=.)); run; proc print data=FreqOut; var Smoking_Cat Weight_Cat Count; run; /* Create heat maps of the tabular results */ ods graphics / width=400px; title "Smoking by Weight Classification"; title2 "Patient Counts (N=5167)"; /* basic heat map */ proc sgplot data=FreqOut; heatmap x=Weight_Cat y=Smoking_Cat / freq=Count discretex discretey colormodel=TwoColorRamp outline; run; /* overlay text and reverse Y axis */ proc sgplot data=FreqOut noautolegend; heatmap x=Weight_Cat y=Smoking_Cat / freq=Count discretex discretey colormodel=TwoColorRamp outline; text x=Weight_Cat y=Smoking_Cat text=Count / textattrs=(size=16pt); yaxis display=(nolabel) reverse; xaxis display=(nolabel); run;