Hi,
I'm totally new to SAS. I want to display a barchart of a question from a survey which can have multiple answers, so I have a table of the different treatments that were selected by the participants and their counts. I used the new define statgraph feature to generate the barchart instead of using the stat vertical bar chart. I used the template from the documentation. I would like to NOT display the "treatment" in the xaxis because the labels of the variables are self-explanatory. Below is what I have. What code do I need to have to tell it not display the label?
Thanks.
proc template;
define statgraph barchart;
begingraph;
entrytitle "What cacner treatment(s) have you had?";
layout overlay;
barchart category=treatment response=frequency /
stat=mean orient=horizontal;
endlayout;
endgraph;
end;
proc sgrender data=sasdata.augtx2021 template=barchart;
run;
Hi @katslau, it helps if you also include your data and/or your graph when you make a post.
If you do not want to display the treatment on the x-axis then you can use the DISPLAY option to select the parts of the XAXIS that you want to display. Try this code below:
proc template;
define statgraph barchart;
begingraph;
entrytitle "What cancer treatment(s) have you had?";
layout overlay / XAXISOPTS = (DISPLAY=(LINE TICKS TICKVALUES));
barchart category=treatment response=frequency /
stat=mean orient=horizontal;
endlayout;
endgraph;
end;
proc sgrender data=sasdata.augtx2021 template=barchart;
run;
Hi @katslau, it helps if you also include your data and/or your graph when you make a post.
If you do not want to display the treatment on the x-axis then you can use the DISPLAY option to select the parts of the XAXIS that you want to display. Try this code below:
proc template;
define statgraph barchart;
begingraph;
entrytitle "What cancer treatment(s) have you had?";
layout overlay / XAXISOPTS = (DISPLAY=(LINE TICKS TICKVALUES));
barchart category=treatment response=frequency /
stat=mean orient=horizontal;
endlayout;
endgraph;
end;
proc sgrender data=sasdata.augtx2021 template=barchart;
run;
Yes, that works.
Thank you.
Hi @djrisks ,
One more question (it's probably very trivia to you): If I want to add the value of the counts to the bar (i.e. at the top of the bar), how would I do that? Something like this chart:
I attached the table for the treatment data.
Thanks again.
Hi @katslau,
Thanks for attaching the data! 😊
To add the value of the counts to the bar, you can use the BARLABEL=TRUE option within the BARCHART statement. Please see the example code below:
proc template;
define statgraph barchart;
begingraph;
entrytitle "What cancer treatment(s) have you had?";
layout overlay / YAXISOPTS = (DISPLAY=(LINE TICKS TICKVALUES));
barchart category=treatment response=frequency /
stat=mean orient=horizontal BARLABEL=true;
endlayout;
endgraph;
end;
proc sgrender data=sasdata.augtx2021 template=barchart;
run;
Yes, that works. I tried that before emailing you and it didn't work. It must be because I didn't put the label at the appropriate place...
Thanks.
Hello @katslau ,
using a template (with PROC TEMPLATE + PROC SGRENDER) is absolute overkill for the required plot.
I would use a HBAR or VBAR statement in PROC SGPLOT (SG = Statistical Graphics).
Submit the below code to find out how this can be done:
proc sgplot data=sashelp.heart;
title "Smoking_Status 'Distribution'";
hbar Smoking_Status;
keylegend / location=inside position=topright;
yaxis display=(nolabel);
run;
title;
Kind regards,
Koen
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.