BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
xiong
Fluorite | Level 6

I am trying to create the box plot. I want to customize the x axis label and the Key legend Text. How can I

ODS ESCAPECHAR = '^';
ods rtf file = "C:\temp\test.rtf" ; 
ods graphics on / attrpriority=none reset=all width=8.0in height=4.5in border=off;


proc sgplot data=sashelp.class noautolegend;
    styleattrs datacolors=(lightgreen lightblue)
               datacontrastcolors=(blue green)
               datasymbols=(Trianglefilled squarefilled );

    vbox height / category=sex 
                 groupdisplay=cluster 
                 name='vbox'
                 boxwidth=0.2;


    xaxis label="sex"
          offsetmax=0.12 
          labelattrs=(size=9pt weight=bold family='Arial') 
          valueattrs=(weight=bold size=9pt family='Arial');

    yaxis label="height"
          min=-10 max=100 
          values=(-10 to 100 by 10)
          labelattrs=(size=10pt weight=bold)
          valueattrs=(weight=bold size=9pt family='Arial') 
          fitpolicy=none;

    refline 0 / axis=y lineattrs=(color=grey pattern=3);

    keylegend 'vbox' / title="sex"
                     location=outside 
                     position=bottom 
                     valueattrs=(family='Arial' size=10pt)
                     titleattrs=(weight=bold family='Arial' size=10pt) 
                     noborder;
run;
ods rtf close;

achieve this?

Thank you in advance.

xiong_0-1750650567399.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

Give this code a try, you might need to adapt it to your needs. It uses a format to display F as Female and M as Male. It makes use of Proc SQL to count the number F and M and write tem into a macro variable. These macro variables are then used for the legenditems.

proc format;
value $gender
 "F" = "Female"
 "M" = "Male"
 ;
run;

proc sql ;
  select
    sum(sex="F") as fCount
    , sum(sex = "M") as mCount
  into
    :fCount
    , :mCount
  from
    sashelp.class
  ;
quit;
proc sgplot data=sashelp.class noautolegend;
  format sex $gender.;
    styleattrs datacolors=(lightgreen lightblue)
               datacontrastcolors=(blue green)
               datasymbols=(Trianglefilled squarefilled );

    vbox height / category=sex 
                 groupdisplay=cluster 
                 name='vbox'
                 boxwidth=0.2;


    xaxis label="sex"
          offsetmax=0.12 
          labelattrs=(size=9pt weight=bold family='Arial') 
          valueattrs=(weight=bold size=9pt family='Arial');

    yaxis label="height"
          min=-10 max=100 
          values=(-10 to 100 by 10)
          labelattrs=(size=10pt weight=bold)
          valueattrs=(weight=bold size=9pt family='Arial') 
          fitpolicy=none;

    refline 0 / axis=y lineattrs=(color=grey pattern=3);

    legenditem TYPE=text name="fCount" / text="&fCount Females" ;
    
    legenditem TYPE=text name="mCount" / text="&mCount Males" ;
 
    keylegend "fCount"  / 
                     location=outside 
                     position=bottom 
                     noborder
;
    keylegend "mCount"  / 
                     location=outside 
                     position=bottom 
                     noborder
                     ;
     
run;

View solution in original post

2 REPLIES 2
BrunoMueller
SAS Super FREQ

Give this code a try, you might need to adapt it to your needs. It uses a format to display F as Female and M as Male. It makes use of Proc SQL to count the number F and M and write tem into a macro variable. These macro variables are then used for the legenditems.

proc format;
value $gender
 "F" = "Female"
 "M" = "Male"
 ;
run;

proc sql ;
  select
    sum(sex="F") as fCount
    , sum(sex = "M") as mCount
  into
    :fCount
    , :mCount
  from
    sashelp.class
  ;
quit;
proc sgplot data=sashelp.class noautolegend;
  format sex $gender.;
    styleattrs datacolors=(lightgreen lightblue)
               datacontrastcolors=(blue green)
               datasymbols=(Trianglefilled squarefilled );

    vbox height / category=sex 
                 groupdisplay=cluster 
                 name='vbox'
                 boxwidth=0.2;


    xaxis label="sex"
          offsetmax=0.12 
          labelattrs=(size=9pt weight=bold family='Arial') 
          valueattrs=(weight=bold size=9pt family='Arial');

    yaxis label="height"
          min=-10 max=100 
          values=(-10 to 100 by 10)
          labelattrs=(size=10pt weight=bold)
          valueattrs=(weight=bold size=9pt family='Arial') 
          fitpolicy=none;

    refline 0 / axis=y lineattrs=(color=grey pattern=3);

    legenditem TYPE=text name="fCount" / text="&fCount Females" ;
    
    legenditem TYPE=text name="mCount" / text="&mCount Males" ;
 
    keylegend "fCount"  / 
                     location=outside 
                     position=bottom 
                     noborder
;
    keylegend "mCount"  / 
                     location=outside 
                     position=bottom 
                     noborder
                     ;
     
run;
xiong
Fluorite | Level 6

It worked, thank you.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 2 replies
  • 435 views
  • 3 likes
  • 2 in conversation
OSZAR »