Hello all,
I'd like to specfiy different colors for a barchart based on values above and below zero. For example red for greater than zero, green for less than zero, and yellow for zero. I'm relatively new to SAS and using SAS graphics so this may be a rudimentary question.
Here is the code I've used so to produce a chart with uniform color:
ods graphics / reset border=off width=10in height=8in;
proc sgplot data=mydata;
vbar name / response=percent
categoryorder=respdesc datalabel datalabelattrs=(size=10pt)
dataskin=crisp barwidth=0.6 fillattrs=(color=CornFlowerBlue);
title h=16pt bold color=black "Crowding by Name";
yaxis label="% Over/Under" labelattrs=(size=11pt)
values=(-100 to 100 by 5) grid;
xaxis label="Name";
xaxis display=(nolabel noline)
fitpolicy=rotate valuesrotate=vertical valueattrs=(size=11pt);
run;
title;
I believe you are asking for a "range attribute" data set. This is an auxiliary data set referenced by the rattrmap option on the proc statement that contains information. Here is an example using the SASHELP.CLASS data set.
data clrresp; retain id "myid"; length min $ 5 max $ 5; input min $ max $ color $ altcolor $ colormodel1 $ colormodel2 $ colormodel3 $; datalines; _min_ 90 purple purple . . . 90 100 gold gold . . . 100 _max_ . . red orange yellow ; run; proc sgplot data=sashelp.class rattrmap=clrresp; vbar age / colorresponse=weight rattrid=myid response=weight stat=mean colorstat=mean; run;
I believe you are asking for a "range attribute" data set. This is an auxiliary data set referenced by the rattrmap option on the proc statement that contains information. Here is an example using the SASHELP.CLASS data set.
data clrresp; retain id "myid"; length min $ 5 max $ 5; input min $ max $ color $ altcolor $ colormodel1 $ colormodel2 $ colormodel3 $; datalines; _min_ 90 purple purple . . . 90 100 gold gold . . . 100 _max_ . . red orange yellow ; run; proc sgplot data=sashelp.class rattrmap=clrresp; vbar age / colorresponse=weight rattrid=myid response=weight stat=mean colorstat=mean; run;
Here's the successful solution:
data bardata;
retain id "myid";
length min $ 5 max $ 5;
input min $ max $ color $altcolor $ colormodel1 $ colormodel2 $ colormodel3 $;
datalines;
_min_ 0 green . . . .
0 0 gold gold . . .
1 _max_ red . . . .
;
run;
title h=16pt bold color=black "Crowding";
proc sgplot data=mydata rattrmap=bardata;
vbar name / colorresponse=rcp rattrid=myid
response=rcp categoryorder=respdesc datalabel datalabelattrs=(size=10pt)
dataskin=crisp barwidth=0.6
stat=mean colorstat=mean;
yaxis label="% Over/Under" labelattrs=(size=11pt)
values=(-100 to 100 by 5) grid;
xaxis label="Name";
xaxis display=(nolabel noline)
fitpolicy=rotate valuesrotate=vertical valueattrs=(size=11pt);
run;
title;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.