How do I make a bar chart with N on the y-axis, a category on the x-axis and percent as a label on the bars without creating an output data set with proc freq or something similiar. Just using raw data. . .
Thank you.
The hacky method that comes to mind is overlaying two plots, one with invisible bars that shows the percentages on a non-shown dual axis. So the labels will come from one plot and the data/bars from another. You will need to customize your legend using keylegend - I'm leaving that for you.
proc sgplot data=sashelp.class;
hbar sex / stat=freq;
hbar sex / stat=percent x2axis datalabel nooutline;
x2axis display=none;
run;
@telligent wrote:
How do I make a bar chart with N on the y-axis, a category on the x-axis and percent as a label on the bars without creating an output data set with proc freq or something similiar. Just using raw data. . .
Thank you.
FYI - replace HBAR with VBAR and X2AXIS with Y2AXIS for a vertical layout.
The hacky method that comes to mind is overlaying two plots, one with invisible bars that shows the percentages on a non-shown dual axis. So the labels will come from one plot and the data/bars from another. You will need to customize your legend using keylegend - I'm leaving that for you.
proc sgplot data=sashelp.class;
hbar sex / stat=freq;
hbar sex / stat=percent x2axis datalabel nooutline;
x2axis display=none;
run;
@telligent wrote:
How do I make a bar chart with N on the y-axis, a category on the x-axis and percent as a label on the bars without creating an output data set with proc freq or something similiar. Just using raw data. . .
Thank you.
FYI - replace HBAR with VBAR and X2AXIS with Y2AXIS for a vertical layout.
Thank you!
proc sgplot data=one noautolegend;
vbar q1 / stat=freq;
xaxis display=(nolabel) ;
vbar q1 /stat=percent x2axis datalabel nooutline;
x2axis display=none;
yaxis integer max=&n;
run;
I noticed the statement below that I added throws the whole thing off. When I do this the bar heights are wrong relative to the y axis. Do you have any suggestions for me? I want the y-axis to go up to a certain level and the bar heights to still be correct. Thank you.
yaxis integer max=100;
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!
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.