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.
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;
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;
It worked, thank you.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.