BookmarkSubscribeRSS Feed
deleted_user
Not applicable
How can I change the bar color based upon the value of rate1?
For example, if rate1=>2 I would like the bar to be red and if 0<2 then I would like the bar to be yellow.
proc gchart data=goals;
vbar bu
discrete sumvar=rate1;
maxis=axis1
raxis=axis2
gaxis=axis3
run;
Thank you.
2 REPLIES 2
DanH_sas
SAS Super FREQ
I would do this by copying rate1 to a new column (we'll say rate2) and put a user-defined format on rate using your range specifications. Then, specify two PATTERN statements with the colors you want (yellow first, then red). Sort your data by rate2 from least to greatest. Set rate2 as the SUBGROUP variable on your VBAR statement. Hopefully, that will give you the result you want.

Thanks!
Dan
GraphGuy
Meteorite | Level 14
Unfortunately, gchart does not let you directly specify specific colors for specific values or ranges of values (you could do that in SAS/AF charts with the "color range object", but not in SAS/Graph charts).

The easiest way I know of to do that, is to add an extra variable to your data set, and use if/else statements to assign a value to that variable using if/else statements based on the >= rules you mentioned.

Then in the gchart, use the subgroup= option, and specify that new variable. The values of this variable will get mapped to the colors in the pattern statement (you have to get a little 'tricky' if your data does not always contain obsns for all the desired colors, by the way).

Here is an example, showing the basics:

data goals;
input bu rate1;
datalines;
1 2.6
2 .5
;
run;

data goals; set goals;
if rate1>=2 then colorval=1;
else colorval=2;
run;

pattern1 v=solid c=red;
pattern2 v=solid c=yellow;

proc gchart data=goals;
vbar bu / discrete sumvar=rate1 subgroup=colorval
refline=2 clipref nolegend;
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 3208 views
  • 0 likes
  • 3 in conversation