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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
DanH_sas
SAS Super FREQ

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;

View solution in original post

4 REPLIES 4
DanH_sas
SAS Super FREQ

What version of SAS are you using?

AD
Calcite | Level 5 AD
Calcite | Level 5

According to Proc Product_status (I'm using EG), the SAS version is 9.3_M1

DanH_sas
SAS Super FREQ

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;

AD
Calcite | Level 5 AD
Calcite | Level 5

Awesome, thanks! Works perfectly.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 8517 views
  • 0 likes
  • 2 in conversation