I want to annotate a boxplot to have some bars in one color and some in another. I try with the code below but it's not working. Why? What I want is to have last two bars (for year > 2018) red, while all other green
/* Sample dataset*/ data test; call streaminit(123); do year= 2010 to 2020; do i = 1 to 100; year = year; var1 = RAND('UNIForm', 30, 100); output; end; end; run; /* Create groups based on year*/ data test; set test; group = (year > 2018); run; /* Specify colors*/ data my_colors; input id $ value fillcolor $; datalines; myid 0 CX96ceb4 myid 1 CXff6f69 ; run; /* Annotated box plot*/ proc sgplot data = test dattrmap=my_colors; vbox var1/ category = year nooutliers nomean attrid = myid; run;
When I try to set colors "by hand" for all bars, it works OK:
proc sgplot data = test; vbox var1/ category = year nooutliers nomean fillattrs=(color = "CX96ceb4"); run;
You're missing the GROUP=group on the VBOX statement needed to look up the values in the attrmap. I also had to retype the "myid" values in the attrmap, as it appeared to have hidden characters that caused a mismatch. But, that could have been a cut-and-paste artifact.
Hope this helps!
I also had to retype the "myid" values in the attrmap, as it appeared to have hidden characters that caused a mismatch.
Indeed, the indentation of that data step was achieved by a leading tab character. I hadn't even noticed these tabs as the Enhanced Editor (unlike the SAS Studio interface, I think) treats them as blanks.
@chris2377: It's better to avoid tab characters in SAS code. There are option settings like "Insert spaces for tabs" available both in the Enhanced Editor and in SAS Studio to create a specified number of space characters when you press the Tab key.
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.