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 2024

Innovate_SAS_Blue.png

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. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1128 views
  • 1 like
  • 3 in conversation