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

Hi, I am trying to change the location of the datalabels in my plot so that they appear on top of the CIs instead of being at the center (to make nicer graphs) but I cannot find a way to do it.

I also want to modify the first datalabel so that instead of a '1' I can write '1 [Reference]' Is this option available in SAS 9.3? Thanks!

data x;

input ct ucl lcl hr;

cards ;    

1     1     1     1

2     2  0.3   1

3     1.8   0.1  0.9

4     1.6  0.2  0.8

run;

proc template;

Define style styles.mystyle;

  Parent=styles.journal;

   Style GraphTitleText / color=black fontsize=13pt fontfamily="swiss" fontweight=bold;

   Style GraphValueText / color=black fontsize=10pt fontfamily="swiss" fontweight=medium;

   Style GraphLabelText / color=black fontsize=10pt fontfamily="swiss" fontweight=medium;

   style GraphDataText from GraphDataText /fontsize=10pt;

   end;

run;

ods html style=mystyle;

title "Mortality by BMI categories";

*title2 'Hazards Ratio and 95% CI';

proc sgplot data=x noautolegend;

  scatter y=hr x=ct /

          yerrorupper=ucl yerrorlower=lcl

          datalabel=hr markerattrs=(size=10 symbol=squarefilled color=black)

              errorbarattrs=(color=black);

            refline 1  / axis=y;

yaxis minor values=(0.1 to 2.3 by 0.3) label='Hazard Ratio';

xaxis minor values=(1 to 4 by 1) label='BMI' ;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
Jay54
Meteorite | Level 14

You can use a different column for labels.  Also you could compute the location just above the ucl, as long as you are ok setting the value in data coordinates.  If the first one is really a reference value, it need not be in the data, and you can use a reference line as suggested by Anca.

ScatterLabel1.png

data x;

length hrlabel $15;

input ct ucl lcl hr;

hrlabel=put(hr, 3.1);

if _n_ = 1 then hrlabel="1 [Reference]";

labelloc=ucl+0.1;

cards ;    

1     1     1     1

2     2  0.3   1

3     1.8   0.1  0.9

4     1.6  0.2  0.8

run;

proc print; run;

proc template;

Define style styles.mystyle;

  Parent=styles.journal;

   Style GraphTitleText / color=black fontsize=13pt fontfamily="swiss" fontweight=bold;

   Style GraphValueText / color=black fontsize=10pt fontfamily="swiss" fontweight=medium;

   Style GraphLabelText / color=black fontsize=10pt fontfamily="swiss" fontweight=medium;

   style GraphDataText from GraphDataText /fontsize=10pt;

   end;

run;

ods html style=mystyle;

ods listing close;

ods listing;footnote;

title "Mortality by BMI categories";

*title2 'Hazards Ratio and 95% CI';

proc sgplot data=x noautolegend;

  scatter y=hr x=ct /

          yerrorupper=ucl yerrorlower=lcl

          markerattrs=(size=10 symbol=squarefilled color=black)

              errorbarattrs=(color=black);

  scatter x=ct y=labelloc / markercharacter=hrlabel;

  refline 1  / axis=y;

  yaxis minor values=(0.1 to 2.3 by 0.3) label='Hazard Ratio';

  xaxis minor values=(1 to 4 by 1) label='BMI' ;

run;

View solution in original post

4 REPLIES 4
AncaTilea
Pyrite | Level 9

Hi.

I don't know how to change the location of the data labels Smiley Sad

However, to modify the first data label so that instead of a '1' to get '1 [Reference]'

this is what i suggest:

data x;

    set x;

     if hr = 1 & ucl = 1 & lcl = 1 then label = . ;

     else label = hr;

run;

/*more of your code*/


/*Then I changed the datalabel value to the 'label' variable:*/

proc sgplot data=x noautolegend tmplout = "&plot_path.\tmpl.sas";

  scatter y=hr x=ct /yerrorupper=ucl yerrorlower=lcl

          datalabel=label datalabelattrs = (size=10 color=black)

            markerattrs=(size=10 symbol=squarefilled color=black)

            errorbarattrs=(color=black);

refline 1/ label=("1 [Reference]")axis = y labelloc = inside labelpos = min;

yaxis minor values=(0.1 to 2.3 by 0.3) label='Hazard Ratio';

xaxis minor values=(1 to 4 by 1) label='BMI' ;

run;

Good luck!

Anca.

DanH_sas
SAS Super FREQ

You can put the labels over the UCL by making two changes:

1. In the data step, make two columns for label position and label content. The label position is derived from the UCL value.

data x;

input ct ucl lcl hr;

if (ucl = hr) then labelpos = ucl + 0.05;

else labelpos = ucl + 0.025;

label = put(hr, 3.1);

cards ;

1  1    1    1

2  2    0.3  1

3  1.8  0.1  0.9

4  1.6  0.2  0.8

run;

2. Instead of using the DATALABEL option on the scatter plot, add another scatter plot with the MARKERCHAR option to plot the label over the UCL.

proc sgplot data=x noautolegend;

  scatter y=hr x=ct /

          yerrorupper=ucl yerrorlower=lcl

          markerattrs=(size=10 symbol=squarefilled color=black)

           errorbarattrs=(color=black);

  refline 1  / axis=y;

  scatter y=labelpos x=ct / markerchar=label;

  yaxis minor values=(0.1 to 2.3 by 0.3) label='Hazard Ratio';

  xaxis minor values=(1 to 4 by 1) label='BMI' ;

run;

As for the content of the first label, you can modify the first value in the "label" column in the data step processing.

Hope this helps!

Dan

Jay54
Meteorite | Level 14

You can use a different column for labels.  Also you could compute the location just above the ucl, as long as you are ok setting the value in data coordinates.  If the first one is really a reference value, it need not be in the data, and you can use a reference line as suggested by Anca.

ScatterLabel1.png

data x;

length hrlabel $15;

input ct ucl lcl hr;

hrlabel=put(hr, 3.1);

if _n_ = 1 then hrlabel="1 [Reference]";

labelloc=ucl+0.1;

cards ;    

1     1     1     1

2     2  0.3   1

3     1.8   0.1  0.9

4     1.6  0.2  0.8

run;

proc print; run;

proc template;

Define style styles.mystyle;

  Parent=styles.journal;

   Style GraphTitleText / color=black fontsize=13pt fontfamily="swiss" fontweight=bold;

   Style GraphValueText / color=black fontsize=10pt fontfamily="swiss" fontweight=medium;

   Style GraphLabelText / color=black fontsize=10pt fontfamily="swiss" fontweight=medium;

   style GraphDataText from GraphDataText /fontsize=10pt;

   end;

run;

ods html style=mystyle;

ods listing close;

ods listing;footnote;

title "Mortality by BMI categories";

*title2 'Hazards Ratio and 95% CI';

proc sgplot data=x noautolegend;

  scatter y=hr x=ct /

          yerrorupper=ucl yerrorlower=lcl

          markerattrs=(size=10 symbol=squarefilled color=black)

              errorbarattrs=(color=black);

  scatter x=ct y=labelloc / markercharacter=hrlabel;

  refline 1  / axis=y;

  yaxis minor values=(0.1 to 2.3 by 0.3) label='Hazard Ratio';

  xaxis minor values=(1 to 4 by 1) label='BMI' ;

run;

AM2323
Calcite | Level 5

Thanks Anca, Dan, and Sanjay. Problem solved and nice graphs ready to present!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 6858 views
  • 5 likes
  • 4 in conversation