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;
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;
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;
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.
You're welcome @PaigeMiller . Yes, that's true about the formatted values.
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
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.