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

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

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