BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PaigeMiller
Diamond | Level 26

When I run this code, for the first BY group (Origin=Asia), the legend shows that SUV is represented by the red circle. In the second BY group (Origin=Europe), SUV is represented by the cyan color circle. How can I get consistent colors across BY groups, so that each group has the same color in each plot?

 

proc sort data=sashelp.cars out=cars;
	by origin;
run;
proc sgplot data=cars;
	styleattrs datacontrastcolors=(red green blue cyan magenta orange black);
	by origin;
	scatter x=msrp y=mpg_city/group=type;
run;

Capture.PNGCapture2.PNG

--
Paige Miller
1 ACCEPTED SOLUTION

Accepted Solutions
djrisks
Barite | Level 11

Hello, you can use Discrete Attribute Maps to achieve the consistency between groups. This is because generally, the attributes for the group are determined by the order that the value is within the data. Using discrete attribute maps can make you indicate the colors that you want to assign to each group value.

 

There are more details here: https://documentation.sas.com/?docsetId=grstatproc&docsetTarget=n18szqcwir8q2nn10od9hhdh2ksj.htm&doc...

 

Here is an example:

proc sort data=sashelp.cars out=cars;
  by origin;
run;

data myattrmap;
  length markercolor $ 7 markersymbol $ 6;
  input id $ value $ markercolor $ markersymbol $;
datalines;
myid Hybrid red circle
myid SUV green circle
myid Sedan blue circle
myid Sports cyan circle
myid Truck magenta circle
myid Wagon orange circle 
;
run;

proc sgplot data=cars dattrmap=myattrmap;
  by origin;
  scatter x=msrp y=mpg_city/group=type attrid=myid;
run;

View solution in original post

4 REPLIES 4
djrisks
Barite | Level 11

Hello, you can use Discrete Attribute Maps to achieve the consistency between groups. This is because generally, the attributes for the group are determined by the order that the value is within the data. Using discrete attribute maps can make you indicate the colors that you want to assign to each group value.

 

There are more details here: https://documentation.sas.com/?docsetId=grstatproc&docsetTarget=n18szqcwir8q2nn10od9hhdh2ksj.htm&doc...

 

Here is an example:

proc sort data=sashelp.cars out=cars;
  by origin;
run;

data myattrmap;
  length markercolor $ 7 markersymbol $ 6;
  input id $ value $ markercolor $ markersymbol $;
datalines;
myid Hybrid red circle
myid SUV green circle
myid Sedan blue circle
myid Sports cyan circle
myid Truck magenta circle
myid Wagon orange circle 
;
run;

proc sgplot data=cars dattrmap=myattrmap;
  by origin;
  scatter x=msrp y=mpg_city/group=type attrid=myid;
run;
PaigeMiller
Diamond | Level 26

Thanks @djrisks. I think I should have thought of that.

 

Anyway, I discovered that when you do this on my real data, which uses custom formats, the value in the MYATTRMAP must be the formatted value, not the unformatted value.

--
Paige Miller
djrisks
Barite | Level 11

You're welcome @PaigeMiller . Yes, that's true about the formatted values.

Rick_SAS
SAS Super FREQ

 

For an introduction to discrete attribute maps, see the articles

 

"Roses are red, violets are blue" by Dan Heath

or 

"Consistent groups colors by value" by Sanjay Matange

For advanced users, see "Automate the creation of a discrete attribute map" by Rick Wicklin

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 4 replies
  • 1968 views
  • 1 like
  • 3 in conversation