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

Hello all,

 

I was wondering if it's possible to change the color of the markers within PROC SGPLOT when you have multiple groups as in the example below.

 

Thank you very much in advance!

 

Recep

 

DATA test;
INPUT Hospital $ age_group $ count;
CARDS;
Hosp1 0-16 0
Hosp1 17-74 10
Hosp1 75+ 1
Hosp2 0-16 14
Hosp3 0-16 1
Hosp3 17-74 16
Hosp4 0-16 1
Hosp4 17-74 4
Hosp5 17-74 7
Hosp6 17-74 80
Hosp6 75+ 2
Hosp7 17-74 4
Hosp8 17-74 9
Hosp9 17-74 95
Hosp9 75+ 1
Hosp10 0-16 1
Hosp10 17-74 27
;
RUN;

proc sgplot data=test;
scatter x=hospital y=count/ group=age_group
grouporder=ascending
MARKERATTRS=(symbol=circlefilled size=10)
LEGENDLABEL= "Hospitalizations"
datalabel
datalabelpos=top;

xaxis display=all fitpolicy=rotate grid;
yaxis offsetmin=0.02 grid;
title ' ';
run;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Do you mean that you want to use specific colors, a different set or something else? The ODS style sets the default colors so changing style changes colors (usually). If you want to override the default color list for that call to the procedure only then that might be the STYLEATTRS statement.

DATACONTRAST colors affect markers. So you might try

 

proc sgplot data=test;
styleattrs
   datacontrastcolors= (red green blue gold)
;
scatter x=hospital y=count/ group=age_group
grouporder=ascending
MARKERATTRS=(symbol=circlefilled size=10)
LEGENDLABEL= "Hospitalizations"
datalabel
datalabelpos=top;

xaxis display=all fitpolicy=rotate grid;
yaxis offsetmin=0.02 grid;
title ' ';
run;

The typical behavior of most of these procedures is the first value encountered gets the color from the list, the second value the second and so on. So data order can affect color/marker/line type etc.

If you want a consistent assignment you need to keep data order consistent, which might mean sorting by a group variable.

Or if you want consistent colors across many procedures you may want to create a DATTRMAP data set.

View solution in original post

3 REPLIES 3
ballardw
Super User

Do you mean that you want to use specific colors, a different set or something else? The ODS style sets the default colors so changing style changes colors (usually). If you want to override the default color list for that call to the procedure only then that might be the STYLEATTRS statement.

DATACONTRAST colors affect markers. So you might try

 

proc sgplot data=test;
styleattrs
   datacontrastcolors= (red green blue gold)
;
scatter x=hospital y=count/ group=age_group
grouporder=ascending
MARKERATTRS=(symbol=circlefilled size=10)
LEGENDLABEL= "Hospitalizations"
datalabel
datalabelpos=top;

xaxis display=all fitpolicy=rotate grid;
yaxis offsetmin=0.02 grid;
title ' ';
run;

The typical behavior of most of these procedures is the first value encountered gets the color from the list, the second value the second and so on. So data order can affect color/marker/line type etc.

If you want a consistent assignment you need to keep data order consistent, which might mean sorting by a group variable.

Or if you want consistent colors across many procedures you may want to create a DATTRMAP data set.

Recep
Quartz | Level 8

Thank you very much! 

styleattrs datacontrastcolors= (red green blue gold); option did exactly what I needed.

I also realized that if VBAR statement is used in the PROC SGPLOT, instead of "datacontrastcolors=(...)", "datacolors=(...)"  will do the trick.

Best,

Recep

ballardw
Super User

@Recep wrote:

Thank you very much! 

styleattrs datacontrastcolors= (red green blue gold); option did exactly what I needed.

I also realized that if VBAR statement is used in the PROC SGPLOT, instead of "datacontrastcolors=(...)", "datacolors=(...)"  will do the trick.

Best,

Recep


Part of the fun is finding out which color set goes with which display.

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