Data visualization with SAS programming

Setting Bar Colors in SAS 9.2

Occasional Contributor VCM
Occasional Contributor
Posts: 16

Setting Bar Colors in SAS 9.2

I have a proc template within a macro.  The data is subset based on the value of VAR (if VAR = 1, or 2, or 3, etc.)  This produces a 5-page bar graph with one VAR group on each page.  The bars are the same color on each page (red), but I want them to be different colors.


I've read some similar threads in this group, which included using DATACONTRASTCOLORS, but that apparently doesn't work in 9.2 as I got an error when I added it.  I need to make the value of the color a macro variable, so I can change it with each call of the macro.


Is there some way to force bar colors in 9.2?

Super User
Posts: 12,148

Re: Setting Bar Colors in SAS 9.2

You really should show the code for the template, assuming that is a template used by SGrender. If you are using another procedure such as SGplot or Sgpanel show that code. With 9.2 you may want to move to Sgrender to have more control.

A little example data may help as well.


If using Sgrender a dynamic variable to pass the color to the template would work.

Posts: 1,227

Re: Setting Bar Colors in SAS 9.2

[ Edited ]

You should be able to use GROUP=VAR in the VBAR statement.  Also set UNIFORM=GROUP in the proc statement.  This will make all bars in one graph with BY=VAR. 


ods _all_  close;
ods html;
proc sort data=sashelp.class out=class;
by  sex;

proc sgplot data=class uniform=group;
  by sex;
  vbar name / response=height group=sex;
ods html close;



With GTL, you can use the INDEX= option.  Create a new column "Index" with values 1 for Males and 2 for Females.  Then set INDEX=index on the BarChart statement. 

Occasional Contributor VCM
Occasional Contributor
Posts: 16

Re: Setting Bar Colors in SAS 9.2

[ Edited ]

Thanks for the input, everyone!


First, the code belongs to my client and I'm not able to share a lot of detail--just the overall logic.

Second, this isn't even my project; I just jumped in to help and now my curiousity has gotten the best of me. Smiley Wink 


The output is 6 pages.  Page 1 is a waterfall all 5 values of VAR, each a different color:

VAR = 1 then red

VAR = 2 then blue

VAR = 3 then yellow

VAR-= 4 then green

VAR = 5 then purple


Pages 2 through 6 are 1 VAR each, and we want the colors to match what is on page 1; so page 2 (VAR = 1) will be red, page 3 (VAR = 2) will be blue, etc.


It seems that the overall issue is how the macros are set up:

  1. A proc template is called to create the style "STYLEX", using "replace GraphData1" etc. to set the 5 colors for page 1.
  2. A proc template is called to create the format for the page 1 waterfall
  3. Proc sgrender creates page 1 from the dataset
  4. A macro is created to subset the dataset by value of VAR (if VAR = &val)
  5. A proc template (still within the macro) is called to create the format for the pages 2-6 waterfalls
  6. Proc sgrender (still within the macro) creates the page
  7. Macro is called 5 times, once for each value of VAR
  8. Another macro combines all 6 pages into one output--and this macro calls STYLEX.  Since pages 2 through 6 only have one value of VAR, each one gets treated like VAR = 1 and is assigned red.


I think what we might need to do is find a way to alter that final macro so that we can create each page in a different style, THEN combine them.

Ask a Question
Discussion stats
  • 3 replies
  • 3 in conversation