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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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;

View solution in original post

3 REPLIES 3
ballardw
Super User

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;
atiedt
Fluorite | Level 6
Thanks! Works like a charm.
atiedt
Fluorite | Level 6

 

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;

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!

SAS Enterprise Guide vs. SAS Studio

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.

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
  • 3 replies
  • 9974 views
  • 1 like
  • 2 in conversation