Hello,
I have a data set with mean values (mean) and an order variable (p).
data temp; input p mean; datalines; 1 41 2 29 3 24 4 24 5 20 6 19 7 19 8 18 9 18 10 16 11 15 12 10 13 7 14 6 ; run;
I want to produce horizontal bars with p on the y axis and the values of mean on the x axis, so I use proc sgplot with hbar (changing the default color of the bars to green)
proc sgplot data=temp noautolegend noborder; hbar p / response=mean fillattrs=(color=green); run;
The result is
This results looks fine to me, however, what can I do when I want that all bars below a mean value of 10 (mean < 10) appear in the color "white"?
Bye, Daniel
Hi @Daniel_Paul
Something like this?
data temp;
input p mean;
if mean < 10 then flag = 1;
else flag = 0;
datalines;
1 41
2 29
3 24
4 24
5 20
6 19
7 19
8 18
9 18
10 16
11 15
12 10
13 7
14 6
;
run;
proc sgplot data=temp noautolegend noborder ;
hbar p / response=mean colorresponse=flag colormodel=(green white);
run;
You want them to be white with a white background?
Only the fill color shoud be white, the border should be black.
Hi @Daniel_Paul
Something like this?
data temp;
input p mean;
if mean < 10 then flag = 1;
else flag = 0;
datalines;
1 41
2 29
3 24
4 24
5 20
6 19
7 19
8 18
9 18
10 16
11 15
12 10
13 7
14 6
;
run;
proc sgplot data=temp noautolegend noborder ;
hbar p / response=mean colorresponse=flag colormodel=(green white);
run;
That's it, thank you.
Alternatively one could make the code a little bit more flexible using formats
Just for fun: an other way to do it using Gchart
proc format;
value ColorM
0 - < 10 = white
10 - < 20 = yellow
20 - < 30 = lightgreen
Other = lightblue
;
run;
data temp;
length Mean_Color $12;
input P Mean;
Mean_Color = put(mean,ColorM.);
datalines;
1 41
2 29
3 24
4 23
5 20
6 19
7 19
8 18
9 18
10 16
11 15
12 13
13 7
14 6
;
run;
goptions device=png;
goptions noborder;
ODS LISTING CLOSE;
ODS HTML path="/home/hgelders/examples" body="Chart_conditional_coloring..htm" ;
goptions gunit=pct htitle=4 ftitle="albany amt/bold" htext=2.5
ftext="albany amt/bold";
goptions ctext=gray33;
axis1 label=none value=(height=2);
axis2 label=('MEAN') order=(0 to 45 by 5) minor=(number=1) offset=(0, 0);
data _Null_; set temp;
by p notsorted;
if first.p then call execute(cats('pattern',_N_,'09'x,'color=',Mean_Color,';'));
run;
title1 ls=1.5 "Gchart with conditional coloring";
proc gchart data=my_data;
hbar p / discrete type=sum sumvar=mean nostats maxis=axis1 raxis=axis2
subgroup=p patternid=subgroup width=2.2 gspace=0.2 autoref clipref cref=graycc coutline=black
nolegend ; run;
quit;
ODS HTML CLOSE;
ODS LISTING;
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.