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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 2735 views
  • 0 likes
  • 3 in conversation