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

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

SGPlot13.png 

 

 

 

 

 

 

 

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 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

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;

Capture d’écran 2020-05-01 à 13.00.15.png

View solution in original post

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

You want them to be white with a white background?

Daniel_Paul
Obsidian | Level 7

Only the fill color shoud be white, the border should be black. 

ed_sas_member
Meteorite | Level 14

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;

Capture d’écran 2020-05-01 à 13.00.15.png

HansGelders
Fluorite | Level 6

Alternatively one could make the code a little bit more flexible using formats 

 

 

HansGelders
Fluorite | Level 6

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 7 replies
  • 5547 views
  • 4 likes
  • 4 in conversation