Hi All,
I am using PROC GCHART and am trying to change the pattern color of my HBAR depending on a variable..
I am posting my code below...
Based on the variable FallRed being 1 or 0 will determine if my bar color should be red = 1 or green <> 1.
I cannot figure out how to incorporate my FallRed 1,0 into my pattern statement to change the color of my bar depending on the 1 or 0.
data Want; set Have;
if (floor = 'Floor 2'
or floor = 'Floor 3'
or floor = 'Floor 4'
AND Fall7 > &falltarget)
then FallRed = 1;
else FallRed = 0;
run;
goptions reset=all device=javaimg xpixels=300 ypixels=220; /*required for dial output */
pattern1 color=Red;/*controls floor 2 bar color */
pattern2 color=Red;/*controls floor 3 bar color */
pattern3 color=Red;/*controls floor 4 bar color */
Axis1
STYLE=1
WIDTH=1
LABEL=NONE /*shows label on axis */
order=("Floor 2" "Floor 3" "Floor 4")
;
Axis2
STYLE=0
WIDTH=1
MAJOR=NONE /*shows major tick marks */
MINOR=NONE /*shows minor tick marks */
LABEL=NONE /*shows label on axis */
VALUE=NONE
;
TITLE &titlstuf "Number of Falls (Last 7 Days)";
PROC GCHART DATA=HAVE
;
HBAR
Floor
/discrete type=sum
SUMVAR=Fall7 patternid=midpoint
CLIPREF
space=10
NOFRAME TYPE=SUM
OUTSIDE=SUM
COUTLINE=SAME
MAXIS=AXIS1
RAXIS=AXIS2
;
QUIT;
Gchart and Gplot tend to assign the first pattern or symbol statement values to the first encountered value, second and others likewise. As such unless you 1) sort the data by the variable of interest to set the order and 2) ALWAYS have the same values in the variable your request tends to be moderately awkward to accomplish at best.
The Sgplot and Sgpanel procedures, as well as graph templates you create, can use a special data set called a DATTRMAP, for discrete attribute map, to consistently display the same color/ marker/ line type/ pattern fill for a give value for a variable used as a Group variable.
Can you please provide an example on how you would conditionally change the bar colors using DATTRMAP?
data myattrmap; length linecolor $ 9 fillcolor $ 9; input ID $ value $ linecolor $ fillcolor $; datalines; myid F pink pink myid M lightblue lightblue ; run; proc sgplot data=sashelp.class dattrmap=myattrmap; vbar age / response=height group=sex groupdisplay=cluster attrid=myid; run;
There's an example. You can use fill colour changes the bar color the line colour changes the outline of the bars colour.
Edit: add source reference:
@zdeb15 wrote:
Can you please provide an example on how you would conditionally change the bar colors using DATTRMAP?
I am trying something like this:
data dattrmap; set Top7Summarized;
value=Fall7;
length FILLCOLOR $8;
id = 'xcolor';
if FallRed=1 then FILLCOLOR= 'Red'; else FILLCOLOR='Green';
run;
proc sort data=dattrmap; by ID; run;
TITLE &titlstuf "Number of Falls (Last 7 Days)";
proc sgplot data=dattrmap dattrmap=dattrmap noautolegend noborder nowall ; ods graphics / noborder;
Hbar Floor/response=Fall7 attrid=xcolor dataskin=crisp fill ;
run;
This returns the graph I want but the color of the bars remain grey. Am I able to change bar colors based on an if statement? Or can I only hard set the colors?
What you're doing here is conditional formatting based on a a specific value of Floor it seems? You need to uniquely identify the groups somehow. This seems to work for your example data. I suspect it may not work for your real data if you've oversimplified your example data set.
data dattrmap; set want;
value=Floor;
length FILLCOLOR $8;
id = 'floor';
if Floor='Floor 2' then FILLCOLOR= 'Red'; else FILLCOLOR='Green';
output;
keep id value fillcolor;
run;
proc sort data=dattrmap; by ID; run;
TITLE &titlstuf "Number of Falls (Last 7 Days)";
proc sgplot data=want dattrmap=dattrmap noautolegend noborder nowall ; ods graphics / noborder;
Hbar Floor/response=Fall7 group= floor groupdisplay=cluster attrid=floor dataskin=crisp fill
;
run;
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!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.