Here's my code in SAS 9.04 TS1M3. I'd like to have the vbar colors change based on whether they are on or over the target line (green), coming up to the target line (lime-yellow-orange), no where near the target line (red). Currently the vbar colors are based on how close they are to the top of the graph instead of the target line. Any help is appreciated.
data date_visits;
input date $CHAR10. visits 8.;
datalines;
11/01/2016 136
12/01/2016 150
01/01/2017 161
02/01/2017 150
03/01/2017 160
04/01/2017 152
05/01/2017 144
06/01/2017 158
07/01/2017 165
08/01/2017 173
09/01/2017 170
10/01/2017 195
11/01/2017 201
;
proc sgplot data=date_visits NOAUTOLEGEND;
vbar date / response=visits
nostatlabel colorresponse=visits
colormodel=(red yellow green);
refline 150 / axis=y lineattrs=(pattern=2 thickness=2px) label='Target 150';
xaxis discreteorder=data label="Month";
yaxis label="visits" minor max=250;
run;
Something like this. You will no doubt want to tweak my colors and value ranges associated with the colors to be more to your liking. There is some subjectivity in this.
data date_visits;
input date $CHAR10. visits 8.;
datalines;
11/01/2016 136
12/01/2016 150
01/01/2017 161
02/01/2017 150
03/01/2017 160
04/01/2017 152
05/01/2017 144
06/01/2017 158
07/01/2017 165
08/01/2017 173
09/01/2017 170
10/01/2017 195
11/01/2017 201
;
data rangeattrmap;
id = "myID";
length colormodel1 colormodel2 $ 8 min 8;
input max colormodel2;
min = ifn(_n_ = 1, 0, lag(max));
colormodel1 = ifc(_n_ = 1, 'red', lag(colormodel2));
datalines;
130 red
140 orange
150 yellow
201 green
;
proc print; run;
proc sgplot data=date_visits NOAUTOLEGEND rattrmap=rangeattrmap;
vbar date / response=visits nostatlabel colorresponse=visits rattrid=myID;
refline 150 / axis=y lineattrs=(pattern=2 thickness=2px) label='Target 150';
xaxis discreteorder=data label="Month";
yaxis label="visits" minor max=250;
run;
You will want to use a range attribute map.
Examples:
https://blogs.sas.com/content/graphicallyspeaking/2013/04/14/attributes-map-3-range-attribute-map/
I will play with it and I might give a more detailed response later.
Something like this. You will no doubt want to tweak my colors and value ranges associated with the colors to be more to your liking. There is some subjectivity in this.
data date_visits;
input date $CHAR10. visits 8.;
datalines;
11/01/2016 136
12/01/2016 150
01/01/2017 161
02/01/2017 150
03/01/2017 160
04/01/2017 152
05/01/2017 144
06/01/2017 158
07/01/2017 165
08/01/2017 173
09/01/2017 170
10/01/2017 195
11/01/2017 201
;
data rangeattrmap;
id = "myID";
length colormodel1 colormodel2 $ 8 min 8;
input max colormodel2;
min = ifn(_n_ = 1, 0, lag(max));
colormodel1 = ifc(_n_ = 1, 'red', lag(colormodel2));
datalines;
130 red
140 orange
150 yellow
201 green
;
proc print; run;
proc sgplot data=date_visits NOAUTOLEGEND rattrmap=rangeattrmap;
vbar date / response=visits nostatlabel colorresponse=visits rattrid=myID;
refline 150 / axis=y lineattrs=(pattern=2 thickness=2px) label='Target 150';
xaxis discreteorder=data label="Month";
yaxis label="visits" minor max=250;
run;
I would also suggest creating a new variable Delta=Visits-150. Then, you can set ColorResponse=Delta instead of Visits. To get consistent colors based on delta amount (or %), use a RangeAttrMap as suggested by Warren.
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.
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.