Is there a way to tie certain colors to certain values in a stacked bar plot?
I'm creating several bar plots of the same data divided by a variable not represented on the graphs. The stacked bar plot represents the proportion of a variable valued either "yes" or "no" at different time periods (each bar is a different time period).
I'm encountering trouble because some plots have only data with a value of "yes" in the first time period, and others have only data with a value of "no" in the first time period (and others have both values in the same time period). Therefore, efforts to order how colors are assigned to stacked parts of bars aren't working, since the first occurring value isn't the same in every plot.
I could just create each plot separately so I can change how the colors are ordered for each plot, but I'm trying to use a macro. Is there a way to ensure "yes" is always blue and "no" is always red, regardless of whether only yes, only no, or both are represented in the first stacked bar on a plot? Thanks!
Sounds like you want the DATTRMAP= option data set for Prod Sgplot (or Sgpanel).
The data set contains specifically named variables to set attributes for values of group= variables.
Then you add the dataset on the Proc statement with the Dattrmap=<datasetname> option.
Then on the plot(s) that use values from the data set you use an Attrid=idname, where the text for idname is the value of a variable in the set named ID to link the values to the characteristics.
Best to read up on this in the online help as there are a BUNCH of possible variables as you can set text, marker and line properties.
There is even an option named SHOW that affects if the legends generated show only values that appear in the graph or have all the values in the legend regardless if one or more may be missing from the current graph.
A dummy example of the key elements would look something like this:
data work.ynattrmap; id='y_n_id'; input value $ fillcolor :$20; datalines; Yes blue No red ; proc sgplot data=somedata dattrmap=work.ynattrmap; vbar somevar / group=yesnovar attrid=y_n_id; run;
Note that except for few numeric size variables like markersize or linewidth the variables are all character. The VALUE in the data set has to match the formatted value of the data used. So if your variable showing as yes is numeric, like 1 or 0 and uses a Format to display 'Yes' or 'No' then the values in the dattrmap set have to match Yes or No not the numeric. If the values are all lower or upper case then they must match in the Dattrmap set.
You can have multiple IDs in a single set, just make sure that same Id are sequential in the data set.
You can also reference some of the style elements such as GraphdataN, where N are 1, 2, etc to match ODS Style setting in your active style. This option is useful if you may change ODS Styles and means that while the color/marker/line may change in a different ODS style the values will display consistently (and if the style is well designed ) and clearly. If you weren't aware ODS HTML, RTF, PDF and Powerpoint use different styles by default. So if you are making multiple versions of a presentation this may be good idea.
You could analyse what values are present for each graph, and use styleattrs datacolors=( );
to cater for each case.
Edit: dattrmap
as suggested by @ballardw is a better option.
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
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.
Ready to level-up your skills? Choose your own adventure.