Dear SAS programmers
I am creating a lot of stacked bar charts in SAS using sgplot. I have created an example of the type of graph I am creating;
data have;
set sashelp.cars;
run;
proc sgplot data=have;
hbar make/stat=percent group=DriveTrain seglabel;
yaxis type=discrete fitpolicy=none;
run;
I am relatively new to SAS. I observe that there is some sort of default or hiarchical coloring of the stacked charts; blue, red, green, some yellow-ish color etc.
Is there some simple way to change this default coloring, fx to different shades of green?
I have seen other SAS users ask similar questions. Someone suggested that you create a dataset with certain color information and then use attrmap= and attrid= in the sgplot code. However, is there not a simple way of simple stating: use these colors and in this order? My issue is that I would like to create many graphs and not deal with each "bar-variable".
Thank you
use the STYLEATTRS statement:
proc sgplot data=have;
styleattrs datacolors=(lightgreen seagreen darkgreen);
hbar make/stat=percent group=DriveTrain seglabel;
yaxis type=discrete fitpolicy=none;
run;
If you only want to create one graph and every data set contains the same GROUP= values, then this is all you need.
The situation becomes more complicated if you have some data sets that don't have all the categories. I can send links to many articles if your situation is more complicated.
use the STYLEATTRS statement:
proc sgplot data=have;
styleattrs datacolors=(lightgreen seagreen darkgreen);
hbar make/stat=percent group=DriveTrain seglabel;
yaxis type=discrete fitpolicy=none;
run;
If you only want to create one graph and every data set contains the same GROUP= values, then this is all you need.
The situation becomes more complicated if you have some data sets that don't have all the categories. I can send links to many articles if your situation is more complicated.
Thanks a bunch, Rick. This looks promising.
The variable in group= is not constant. The basic difference between the (many) graphs is the variable that I include in group, but more so the population and year that the graph represents.
So, let's say I have 20 different sgplot with each their own specific variable in group=. Some of the variables will have 2 categories and some will have 5 or more. I might make something like 32 different charts (different years, different populations) based on each sgplot code. I don't mind making a specification for styleattrs datacolors for each of the "main" 20 different sgplot codes. Actually, there might be some benefit to doing that. If you see some issue with this I will be happy to hear about it.
I suggest you try the simple method first. If it works, then you are done. If you see an issue, write back and describe the issue. We can suggest more sophisticated methods, but your initial post indicates that you are new to SAS and wish to keep this as simple as possible, at least for now.
I think the simple version will work for now. Thanks.
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 25. Read more here about why you should contribute and what is in it for you!
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.