BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
robby_beum
Quartz | Level 8

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;Capture.GIF

1 ACCEPTED SOLUTION

Accepted Solutions
WarrenKuhfeld
Rhodochrosite | Level 12

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;

View solution in original post

3 REPLIES 3
WarrenKuhfeld
Rhodochrosite | Level 12

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;
Jay54
Meteorite | Level 14

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1518 views
  • 2 likes
  • 3 in conversation