Hi all,
is there any way to change the barwidth
in sgplot histogram statement?
If I understand your question, you want to control the physical width of the bars (not the 'width' of the data bin - ie, not the range of values in each bar) ? ...
In sgplot histograms, I believe the physical width of the bars expands to fill most/all available space. Therefore one way to control the physical width of the bars would be to change the width of the page. (But you probably don't want that.)
You can also add more 'offset' between the bars and the axes, using the xaxis offsetmin= and offsetmax= options, to 'squeeze' the bars into a smaller space and make them 'skinnier' (offsetmin controls the space on the left side, and offsetmax controls the right side). Here's an example, without and with the offsetmin and offsetmax options:
/* default, with no offsetmin/offsetmax specified */
proc sgplot data=sashelp.heart;
where sex=:'F';
histogram agechddiag / nbins=7 fillattrs=(color=pink);
run;
proc sgplot data=sashelp.heart;
where sex=:'F';
histogram agechddiag / nbins=7 fillattrs=(color=pink);
xaxis offsetmin=.2 offsetmax=.2;
run;
use the BINWIDTH= option
Hi thanks, is binwidth=barwidth. This is not giving me the right results, even if I use even when I use
binwidth= binstart= showbins
@Anita_n wrote:
Hi thanks, is binwidth=barwidth. This is not giving me the right results, even if I use even when I use
binwidth= binstart= showbins
Explain further. What is not giving you the right results? What isn't right about them? Show us your code. Show us your output.
proc sgplot data=sashelp.heart;
where sex=:'F';
histogram agechddiag / fillattrs=(color=pink);
run;
/*After adding binwidth*/
proc sgplot data=sashelp.heart;
where sex=:'F';
histogram agechddiag / fillattrs=(color=pink) binwidth=5 binstart=50 showbins;
run;
Please explain what is wrong with the plot, and what you would like to see.
ods select histogram; proc univariate data=sashelp.heart ; where sex=:'F'; var agechddiag ; histogram agechddiag / kernel barwidth=1; run; proc univariate data=sashelp.heart ; where sex=:'F'; var agechddiag ; histogram agechddiag / kernel endpoints=(30 to 100 by 20) ; run;
proc sgplot data=sashelp.heart;
where sex=:'F';
histogram agechddiag / fillattrs=(color=pink) nbins=10 showbins;
run;
proc sgplot data=sashelp.heart;
where sex=:'F';
histogram agechddiag / fillattrs=(color=pink) nbins=5 showbins;
run;
what does this nbins really do? I just wanted to reduce the size of the bars like when using barwidth=0.9 or barwidth=0.6
In a histogram, the bar width is data-driven. The width of the bar shows the range of values included in the bin. By changing the number of bins (NBINS), you change the range of values in each bin. The more bins you have, the thinner the bars will be.
Okay , thanks I now understand how it works
If I understand your question, you want to control the physical width of the bars (not the 'width' of the data bin - ie, not the range of values in each bar) ? ...
In sgplot histograms, I believe the physical width of the bars expands to fill most/all available space. Therefore one way to control the physical width of the bars would be to change the width of the page. (But you probably don't want that.)
You can also add more 'offset' between the bars and the axes, using the xaxis offsetmin= and offsetmax= options, to 'squeeze' the bars into a smaller space and make them 'skinnier' (offsetmin controls the space on the left side, and offsetmax controls the right side). Here's an example, without and with the offsetmin and offsetmax options:
/* default, with no offsetmin/offsetmax specified */
proc sgplot data=sashelp.heart;
where sex=:'F';
histogram agechddiag / nbins=7 fillattrs=(color=pink);
run;
proc sgplot data=sashelp.heart;
where sex=:'F';
histogram agechddiag / nbins=7 fillattrs=(color=pink);
xaxis offsetmin=.2 offsetmax=.2;
run;
@GraphGuy yes, that is exactly what I wanted. Thanks
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.