I have a plot where I would like the datalabels to be positioned directly above the marker. In the code below, sometimes the datalabel is directly above the marker, but most of the time the default seems to be to have the datalabel off to the side of the marker. How can I do this?
Data Cand1;
Input Domain $ Items PercM SE LowerL UpperH;
Cards;
D1 44 86 80 78 94
D2 30 73 10 63 83
D3 38 79 90 70 88
D4 13 69 13 56 82
;
Run;
Proc sort data=cand1;
by descending domain;
run;
proc sgplot data= Cand1 noautolegend nocycleattrs ;
scatter y=Domain x=PercM / datalabel=PercM datalabelattrs=(size=12 weight=bold) markerattrs=(symbol=plus);
scatter y=Domain x=LowerL / datalabel=LowerL datalabelattrs=(size=9) markerattrs=(symbol=plus) ;
scatter y=Domain x=UpperH / datalabel=UpperH datalabelattrs=(size=9) markerattrs=(symbol=plus) ;
highlow y=Domain low=LowerL high=UpperH ;
xaxis label="Percent Correct" values=(0,20,40,60,80,100) ;
yaxis label="Domain" discreteorder=data;
run;
Unfortunately, the DATALABELPOS option for SCATTER plots was not surface in the SG procedures until SAS 9.4. However, The code below is a neat alternative. Basically, it plots the labels separately from the scatter points. The DISCRETEOFFSET option offsets the "labeling" scatter plot to raise the labels above the scatter points. The FORMAT is required to trim the excess space and center the labels.
proc sgplot data= Cand1 noautolegend nocycleattrs;
format PercM LowerL UpperH 2.;
scatter y=Domain x=PercM / markerattrs=(symbol=plus);
scatter y=Domain x=LowerL / markerattrs=(symbol=plus) ;
scatter y=Domain x=UpperH / markerattrs=(symbol=plus) ;
scatter y=Domain x=PercM / markerchar=PercM markercharattrs=(size=12 weight=bold) discreteoffset=0.12;
scatter y=Domain x=LowerL / markerchar=LowerL markercharattrs=(size=9) discreteoffset=0.12;
scatter y=Domain x=UpperH / markerchar=UpperH markercharattrs=(size=9) discreteoffset=0.12;
highlow y=Domain low=LowerL high=UpperH ;
xaxis label="Percent Correct" values=(0,20,40,60,80,100) ;
yaxis label="Domain" discreteorder=data;
run;
What version of SAS are you using?
According to Proc Product_status (I'm using EG), the SAS version is 9.3_M1
Unfortunately, the DATALABELPOS option for SCATTER plots was not surface in the SG procedures until SAS 9.4. However, The code below is a neat alternative. Basically, it plots the labels separately from the scatter points. The DISCRETEOFFSET option offsets the "labeling" scatter plot to raise the labels above the scatter points. The FORMAT is required to trim the excess space and center the labels.
proc sgplot data= Cand1 noautolegend nocycleattrs;
format PercM LowerL UpperH 2.;
scatter y=Domain x=PercM / markerattrs=(symbol=plus);
scatter y=Domain x=LowerL / markerattrs=(symbol=plus) ;
scatter y=Domain x=UpperH / markerattrs=(symbol=plus) ;
scatter y=Domain x=PercM / markerchar=PercM markercharattrs=(size=12 weight=bold) discreteoffset=0.12;
scatter y=Domain x=LowerL / markerchar=LowerL markercharattrs=(size=9) discreteoffset=0.12;
scatter y=Domain x=UpperH / markerchar=UpperH markercharattrs=(size=9) discreteoffset=0.12;
highlow y=Domain low=LowerL high=UpperH ;
xaxis label="Percent Correct" values=(0,20,40,60,80,100) ;
yaxis label="Domain" discreteorder=data;
run;
Awesome, thanks! Works perfectly.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.