Hello all,
This is a follow up to my post about a week ago creating a heat map using HEATMAPPARM to generate a discrete legend. Using the below code, I finally have the exact graph I want.... almost. It appears that by using an attribute map, I am no longer able to apply my formats? Below are my images and code. The teal-brown-purple graph has the correct formats applied, but the colors don't work. The yellow-orange-red one is the color I want, but my legend does not have the format. Would anyone know why these two pieces of code don't want to work together? I am using SAS 9.4 Thank you!
Code WITH format that produces wrong colors:
/*formats for skill bin labels*/
proc format;
value sb
1="0.01-0.05"
2="0.05-0.20"
3="0.20-0.35"
4="0.35-0.50"
5="0.50-1.00";
run;
/*create attribute map for cell colors*/
data Attrs;
length Value 8. FillColor $20;
ID = "skill_bin";
Value = 1; FillColor="cxFFFF66"; output;
Value = 2; FillColor="cxFFCC00"; output;
Value = 3; FillColor="cxFF9900"; output;
Value = 4; FillColor="cxCC3300"; output;
Value = 5; FillColor="cx910014"; output;
run;
/*create heat map for each state/territory of ensemble peak week skill and number of weeks pre/post peak week*/
title "State-specific ensemble model skill for peak week, relative to observed peak week, per state/territory";
ods graphics / height=1100px width=900px;
proc sgplot data=heatmap dattrmap=Attrs;
heatmapparm x=relative_wk y=territory colorgroup=skill_bin / attrid=skill_bin outline discretex discretey;
keylegend / title="Ensemble Peak Week Forecast Skill" across=5 sortorder=ascending titleattrs=(size=8) valueattrs=(size=8);
xaxis type=discrete label="Week Relative to Observed Peak Week" labelattrs=(size=8 weight=bold) valueattrs=(size=8);
yaxis type=discrete label="State/Territory" labelattrs=(size=8 weight=bold) valueattrs=(size=8);
format Skill_bin sb. location loc.;
run;
Code WITHOUT format that produces correct colors, but I lose my legend labels:
/*formats for skill bin labels and state/territory names*/
proc format;
value sb
1="0.01-0.05"
2="0.05-0.20"
3="0.20-0.35"
4="0.35-0.50"
5="0.50-1.00";
run;
/*create attribute map for cell colors*/
data Attrs;
length Value 8. FillColor $20;
ID = "skill_bin";
Value = 1; FillColor="cxFFFF66"; output;
Value = 2; FillColor="cxFFCC00"; output;
Value = 3; FillColor="cxFF9900"; output;
Value = 4; FillColor="cxCC3300"; output;
Value = 5; FillColor="cx910014"; output;
run;
/*create heat map for each state/territory of ensemble peak week skill and number of weeks pre/post peak week*/
title "State-specific ensemble model skill for peak week, relative to observed peak week, per state/territory";
ods graphics / height=1100px width=900px;
proc sgplot data=heatmap dattrmap=Attrs;
heatmapparm x=relative_wk y=territory colorgroup=skill_bin / attrid=skill_bin outline discretex discretey;
keylegend / title="Ensemble Peak Week Forecast Skill" across=5 sortorder=ascending titleattrs=(size=8) valueattrs=(size=8);
xaxis type=discrete label="Week Relative to Observed Peak Week" labelattrs=(size=8 weight=bold) valueattrs=(size=8);
yaxis type=discrete label="State/Territory" labelattrs=(size=8 weight=bold) valueattrs=(size=8);
run;
This one really has me stuck. Is it something as simple as the format needs to go in the attribute map? I admit I'm not very familiar with making one. Thank you in advance!
The attribute maps should refer to the FORMATTED values. Those are the values that determine the groups:
/*create attribute map for cell colors*/
data Attrs;
length Value $10 FillColor $15;
input raw FillColor;
Value = put(raw, SB.); /* use format to assign values */
retain ID "skill_bin";
datalines;
1 cxFFFF66
2 cxFFCC00
3 cxFF9900
4 cxCC3300
5 cx910014
;
When you use a discrete attribute map, you shouldn't need the SORTORDER= option on the KEYLEGEND statement.
The attribute maps should refer to the FORMATTED values. Those are the values that determine the groups:
/*create attribute map for cell colors*/
data Attrs;
length Value $10 FillColor $15;
input raw FillColor;
Value = put(raw, SB.); /* use format to assign values */
retain ID "skill_bin";
datalines;
1 cxFFFF66
2 cxFFCC00
3 cxFF9900
4 cxCC3300
5 cx910014
;
When you use a discrete attribute map, you shouldn't need the SORTORDER= option on the KEYLEGEND statement.
And @Rick_SAS you save the day again! Thank you very much!
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.