BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
chris2377
Quartz | Level 8

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @chris2377,

 

Use the GROUP= option to tell SAS that the values 0, 1 in the attribute map dataset refer to variable group of dataset TEST:

vbox var1/ category = year group = group nooutliers nomean attrid = myid;

View solution in original post

5 REPLIES 5
FreelanceReinh
Jade | Level 19

Hello @chris2377,

 

Use the GROUP= option to tell SAS that the values 0, 1 in the attribute map dataset refer to variable group of dataset TEST:

vbox var1/ category = year group = group nooutliers nomean attrid = myid;
chris2377
Quartz | Level 8

@FreelanceReinh 

 

As simple as that. I simply forgot the group argument. Thanks a lot!

DanH_sas
SAS Super FREQ

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!

Dan

chris2377
Quartz | Level 8

@DanH_sas 

 

Thanks for your reply. After adding "group" option everything works as expected, so it may be the copy-paste issue.

FreelanceReinh
Jade | Level 19

@DanH_sas wrote:

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 540 views
  • 2 likes
  • 3 in conversation