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

Hello, I have code that resembles this:

proc sgplot data=d1;

histogram myvariable/scale=count BINWIDTH=5;

refline 0.3 0.7 1.77 2.78 10/axis=X label=("100%" "82%" "82%" "76%" "37%") LINEATTRS=(color=red);*add the 5 reference lines and labels;

run;

When I run it, it produces the histogram I want, with the reference lines I want.  However, the labels for the reference lines are turned vertically, not horizontal in normal reading fashion.  I could not find any option to turn those labels horizontal.  Is there a way in SAS to do it? Thank you, Nicole

1 ACCEPTED SOLUTION

Accepted Solutions
Jay54
Meteorite | Level 14

Yes, the "Insert Image" feature is turned off as it is often used to spam the site.  It is a problem for us all.

Sometimes the issue can be reproduced using one of the SASHELP data sets like Cars or Heart.

proc sgplot data=sashelp.heart;

  histogram cholesterol;

  refline 200 210 220 / axis=x label=("200" "210" "220") labelattrs=(size=12);

  xaxis max=400;

  run;

proc sgplot data=sashelp.heart;

  histogram cholesterol;

  refline 200 210 220 / axis=x label=("200" "210" "220") labelloc=inside labelattrs=(size=12);

  xaxis max=400;

  run;

With LABELLOC=INSIDE, colliding labels will be drawn,but the results may be less then ideal.

View solution in original post

6 REPLIES 6
Jay54
Meteorite | Level 14

A picture or runnable program would help to see wht is going on.  The default location for labels is outside.  So, if the reference line labels get too close and collide, they all will be rotated.  You can provide more width, or move them inside.

NWV
Calcite | Level 5 NWV
Calcite | Level 5

I would love to attache a picture.  I have a screen shot, saved as a jpg, but when I try and "insert image" to this reply box, it does not work.  I've tried both IE and chrome.  I will try some of your other suggestions, and see if I can get a better result.  The data is not in the public domain, so a runnable program with the same set up would be rather time consuming.  Thank you very much for the response.

Jay54
Meteorite | Level 14

Yes, the "Insert Image" feature is turned off as it is often used to spam the site.  It is a problem for us all.

Sometimes the issue can be reproduced using one of the SASHELP data sets like Cars or Heart.

proc sgplot data=sashelp.heart;

  histogram cholesterol;

  refline 200 210 220 / axis=x label=("200" "210" "220") labelattrs=(size=12);

  xaxis max=400;

  run;

proc sgplot data=sashelp.heart;

  histogram cholesterol;

  refline 200 210 220 / axis=x label=("200" "210" "220") labelloc=inside labelattrs=(size=12);

  xaxis max=400;

  run;

With LABELLOC=INSIDE, colliding labels will be drawn,but the results may be less then ideal.

NWV
Calcite | Level 5 NWV
Calcite | Level 5

As you suggest, with the LABELLOC=INSIDE, colliding labels are drawn, but they overlap.  I did try the labelattrs=(size=12), but that option does not appear to work in my refline statement.  Not sure if that is just me, or a base SAS thing.  I am running SAS 9.3 TS Level 1m2 W32_7 Pro platform.  I will revisit the data a decide if some of the reference lines can be dropped.  But thank you very much for the bit about label location, because I never would have guessed that would turn the labels horizontal, into normal reading angle.

Jay54
Meteorite | Level 14

Technically, LABELLOC does not "turn" the labels to horizontal.  It just draws the label in the normal way, and if they collide, too bad.  The default LABELLOC=OUTSIDE does extra work for you to avoid collision between labels and any potential axis labels too.  If such collision is detected, all labels are turned sideways, or at an angle.

You don;t really need the LABELATTRS=(SIZE=12).  I did that to trigger the collisions.  This option may have been added at SAS 9.40 or later.

If these labels are a must, one possibility is to stagger alternate labels top and bottom using LABELPOS:

proc sgplot data=sashelp.heart;

  histogram cholesterol;

  refline 200 220 / axis=x label=("200" "220") labelloc=inside labelattrs=(size=12);

  refline 210 230 / axis=x label=("210" "230") labelloc=inside labelpos=min labelattrs=(size=12);

  xaxis max=400;

  run;

NWV
Calcite | Level 5 NWV
Calcite | Level 5

That is an excellent suggestion, I will try it out.  The author wants the labels on there, no way to get around it!

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
  • 6 replies
  • 2388 views
  • 3 likes
  • 2 in conversation