Using ANNOTATE to color labels dependent on data. Attempting to color the markers as well. I've added Function='symbol' to the annotate dataset, but am missing something as there are no changes to the markers.
FUNCTION='symbol';
size=5;
text='dot';
position='5';
style='marker';
Data and working code (other than marker color) is below.
DM LOG 'CLEAR';
DM OUTPUT 'CLEAR';
DATA plotme3;
LENGTH Location $10. WeekEnding 8. NetPromoter Threshold Target Maximum 8.;
FORMAT WeekEnding mmddyy10. NetPromoter Threshold Target Maximum percent5.1;
INPUT Location WeekEnding DATE9. NetPromoter Threshold Target Maximum;
CARDS;
Blanchard 23JUN2018 . 0.845 0.861 0.878 Blanchard 07JUL2018 0.667 0.845 0.861 0.878 Blanchard 21JUL2018 0.872 0.845 0.861 0.878 Blanchard 04AUG2018 0.904 0.845 0.861 0.878 Blanchard 18AUG2018 0.852 0.845 0.861 0.878 Blanchard 01SEP2018 0.72 0.845 0.861 0.878 Blanchard 15SEP2018 0.667 0.845 0.861 0.878 Blanchard 29SEP2018 0.95 0.845 0.861 0.878 Blanchard 13OCT2018 0.833 0.845 0.861 0.878 Blanchard 27OCT2018 0.763 0.845 0.861 0.878 Blanchard 10NOV2018 0.821 0.845 0.861 0.878 Blanchard 24NOV2018 0.863 0.845 0.861 0.878 Blanchard 08DEC2018 0.6 0.845 0.861 0.878 Blanchard 22DEC2018 . 0.845 0.861 0.878 RUN;
goptions reset=all;
* prevent surprises from other options being set;
ods noresults;
* keep adobe from opening inside SAS session;
OPTIONS orientation=landscape nodate nonumber;
* create annotate dataset to add labels to the three targets;
* annotate will also color-code the labels and symbols;
DATA anno(KEEP=function x y text style color position size hsys xsys ysys when);
LENGTH function color $8. text $16.;
retain hsys xsys ysys "2" when 'a';
FORMAT x mmddyy10.;
SET plotme3 end=eof;
IF NetPromoter < .845 THEN
DO;
* lowest ranking, text and marker should be red;
FUNCTION='label';
x=WeekEnding;
y=NetPromoter;
color='red';
position='4';
text=TRIM(LEFT(put(NetPromoter, percent5.1))) || "--";
OUTPUT;
FUNCTION='symbol';
size=5;
text='dot';
position='5';
style='marker';
OUTPUT;
*<< not working as expected;
END;
ELSE IF NetPromoter < .861 THEN
DO;
* met threshold, text and marker should be yellow;
FUNCTION='label';
x=WeekEnding;
y=NetPromoter;
color='yellow';
position='4';
text=TRIM(LEFT(put(NetPromoter, percent5.1))) || "--";
OUTPUT;
FUNCTION='symbol';
size=5;
text='dot';
position='5';
style='marker';
OUTPUT;
*<< not working as expected;
END;
ELSE IF NetPromoter < .878 THEN
DO;
* met target, text and marker should be green;
FUNCTION='label';
x=WeekEnding;
y=NetPromoter;
color='green';
position='4';
text=TRIM(LEFT(put(NetPromoter, percent5.1))) || "--";
OUTPUT;
FUNCTION='symbol';
size=5;
text='dot';
position='5';
style='marker';
OUTPUT;
*<< not working as expected;
END;
ELSE
DO;
* highest ranking, text and marker should be magenta;
FUNCTION='label';
x=WeekEnding;
y=NetPromoter;
color='magenta';
position='4';
text=TRIM(LEFT(put(NetPromoter, percent5.1)));
OUTPUT;
FUNCTION='symbol';
size=5;
text='dot';
position='5';
style='marker';
OUTPUT;
*<< not working as expected;
END;
IF eof THEN
DO;
* create labels;
style='Albany';
FUNCTION="label";
x=WeekEnding;
hsys='D';
position='1';
size=15;
color="magenta";
y=.878;
text='Maximum: 87.8%';
OUTPUT;
color="green";
y=.861;
text='Target: 86.1%';
OUTPUT;
color="yellow";
y=.845;
text='Threshold: 84.5%';
OUTPUT;
END;
RUN;
PROC SORT DATA=anno;
WHERE text NE '.';
BY function x y;
RUN;
* creates better spacing on the graph;
* f = font, w = width in pixels, h = height in cells, l = line type (1 is solid, 2-46 dashed lines), v=value (plus, x star, square diamond dot circle), i = interpolation (none, join, spline);
symbol1 v=dot cv=green ci=black i=join l=1 h=2;
symbol2 c=magenta i=join l=3 w=2;
symbol3 c=green i=join l=3 w=2;
symbol4 c=yellow i=join l=3 w=2;
PROC GPLOT DATA=plotme3;
FORMAT NetPromoter Maximum target Threshold percent5.1;
PLOT NetPromoter * WeekEnding=1 Maximum*WeekEnding=2 Target*WeekEnding=3
Threshold*WeekEnding=4 /OVERLAY ch=purple lh=3 skipmiss haxis=axis1
vaxis=axis2 cframe=ligr annotate=anno;
AXIS1 LABEL=(height=1 font=albany color=black '2-Week Period Ending Date')
VALUE=(height=1 font=albany color=black);
AXIS2 LABEL=(angle=90 height=1.0 font=albany color=black 'Net Promoter Score')
VALUE=(height=1 font=albany color=black);
TITLE h=2 f=albany "Net Promoter Score by Pay Period";
RUN;
QUIT;
I am going to attach an example I used for introducing some fellow workers to similar topics.
Proc Gplot is not getting much enhancement with all the main work going into Proc Sgplot, Sgpanel and GTL solutions.
The example in the attachment has a small data set used with a number of plots of different types and references creating a DATTRMAP data set, discrete attributes for graph items using a GROUP variable with a number of options and using your active ODS style as the base for line, marker, fill and text color.
You can get a lot done with far less work using sgplot. Quick demo:
DATA plotme3;
LENGTH Location $10. WeekEnding 8. NetPromoter Threshold Target Maximum 8.;
FORMAT WeekEnding mmddyy10. NetPromoter Threshold Target Maximum percent5.1;
INPUT Location WeekEnding DATE9. NetPromoter Threshold Target Maximum;
IF NetPromoter < .845 THEN group=1;
ELSE IF NetPromoter < .861 THEN group=2;
ELSE IF NetPromoter < .878 THEN group=3;
ELSE group=4;
CARDS;
Blanchard 23JUN2018 . 0.845 0.861 0.878
Blanchard 07JUL2018 0.667 0.845 0.861 0.878
Blanchard 21JUL2018 0.872 0.845 0.861 0.878
Blanchard 04AUG2018 0.904 0.845 0.861 0.878
Blanchard 18AUG2018 0.852 0.845 0.861 0.878
Blanchard 01SEP2018 0.72 0.845 0.861 0.878
Blanchard 15SEP2018 0.667 0.845 0.861 0.878
Blanchard 29SEP2018 0.95 0.845 0.861 0.878
Blanchard 13OCT2018 0.833 0.845 0.861 0.878
Blanchard 27OCT2018 0.763 0.845 0.861 0.878
Blanchard 10NOV2018 0.821 0.845 0.861 0.878
Blanchard 24NOV2018 0.863 0.845 0.861 0.878
Blanchard 08DEC2018 0.6 0.845 0.861 0.878
Blanchard 22DEC2018 . 0.845 0.861 0.878
;
proc sgplot data=plotme3 noautolegend;
series x=weekending y=netpromoter;
scatter x=weekending y=netpromoter / group=group datalabel=netpromoter
markerattrs=(symbol=circlefilled);
format netpromoter percent5.0;
run;
Tech Support to the rescue! Continued with GPLOT. Solution was to modify HSYS from "2" to "4" and change TEXT to 'W' rather than "DOT". Have searched SAS docs to identify where 'W' came from and have not found an answer.
Regardless, I do have color-coded markers and labels that change based on the region they are plotted within.
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!
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.