Recently a user posted a query on the SAS Communities page asking on how to create a histogram where the bins of the histogram are colored by the analysis variable using a three color ramp. Essentially, he wanted the bins to be colored from "Low" to "High" along the horizontal axis.

The benefit of coloring the bins of the histogram in this manner was not immediately clear to me, but i assume the user had a good reason. So I gave some thought to how we might create such a graph using the SAS 9.3 SGPLOT procedure. The result user wanted is shown on the right.

Clearly, the SGPLOT Histogram statement does not support gradient coloring at any release, including SAS 9.40M3. So, one way to do this is to use the VBAR statement with the GROUP option, where the group colors are obtained from the Discrete Attributes Map. There may be other ways too, and I would be happy to hear your ideas.

Step 1 is to bin the data, in this case the Cholesterol column in the sashelp.heart data set. I have selected a bin interval of 10 and computed another variable ChBin whose values is between 90 and 360. The min and max value of bin are saved in the macro variables.

*/*--Bin the data by cholesterol--*/*

* data ChBin;*

* label ChBin='Cholesterol';*

* retain BinInt 10 maxbin 0 minbin 1e6;*

* set sashelp.heart(where=(cholesterol > 80 and cholesterol < 360) *

* keep=Cholesterol Systolic) end=last;*

* if cholesterol ne . then ChBin=BinInt*floor(cholesterol/BinInt);*

* minbin=min(minbin, chbin);*

* maxbin=max(maxbin, chbin);*

* if last then do;*

* call symput("MinBin", minbin);*

* call symput("MaxBin", maxbin);*

* call symput("BinInt", BinInt);*

* end;*

* run;*

Now, we can plot a histogram of this data by using the VBAR statement as follows. Since this is really a bar chart, the x axis is discrete, and each tick value is displayed. I reduced the font to prevent the rotation of each value. Click on the graph for a higher resolution image.

*/*--Histogram using Bar Chart SAS 9.3--*/*

*proc sgplot data=ChBin noautolegend;*

* vbar chbin / barwidth=0.9 nooutline;*

* xaxis valueattrs=(size=6);*

* run;*

Now we will color the bars using GROUP=ChBin option, so each bar will be colored individually. We need to compute a color for each bin value using the "Green" to "Yellow" to "Red" ramp. We will compute these values and load the Discrete Attributes Map programmatically as follows.

*/*--Define attributes map data set--*/*

*data AttrMap;*

* length FillColor $8 LineColor $8;*

* id='Hist';*

* ghigh=192; /*--High value for Green--*/*

* rhigh=255; /*--High value for Red--*/*

* mid=(&minbin + &maxbin) / 2;*

* LineColor='CX000000';*

* do val=&minbin to &maxbin by &BinInt;*

* value=put(val, 5.0);*

* if val < mid then do;*

* g=ghigh; b=0; r=rhigh*(val-&minbin)/ (mid-&minbin);*

* end;*

* else do;*

* r=rhigh; b=0; g=ghigh*(1-((val-&minbin) - (mid-&minbin))/ (mid-&minbin));*

* end;*

* fillcolor='CX' || put(r, hex2.) || put(g, hex2.) || put(b, hex2.); *

* output;*

* end;*

*run;*

The color starts at green (CX00C000) and ends at red (CXFF0000). Each "Value" and "FillColor" is saved into the AttrMap data set. Then, we use this with the VBAR with GROUP=ChBin and AttrId=Hist.

*/*--Histogram using Bar Chart group colors SAS 9.3--*/*

*proc sgplot data=ChBin dattrmap=AttrMap noautolegend;*

* vbar chbin / barwidth=0.9 group=chbin attrid=Hist nooutline;*

* xaxis valueattrs=(size=6);*

* run;*

How easy is this with SAS 9.40M3? One still need to do the binning, but after that, we can just use the ColorResponse=ChBin option with Stat=Mean. The ColorModel can be easily set using the same three colors.

Note also the added benefit of setting a "Linear" x-axis with the VBAR. This is now supported with SAS 9.40M3, providing us a nice interval axis without the clutter of a "Bin" axis with all values.

*/*--Histogram using Bar Chart response colors SAS 9.40M3--*/*

*proc sgplot data=ChBin noautolegend;*

* vbar chbin / barwidth=0.9 colorresponse=chbin colorstat=mean *

* colormodel=(cx00C000 cxFFC000 cxff0000) nooutline;*

* xaxis type=linear;*

* run;*

**Full program: Resp_Color_Histogram**