BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Anita_n
Pyrite | Level 9

Hi all,

is there any way to change the barwidth

in sgplot histogram statement?

1 ACCEPTED SOLUTION

Accepted Solutions
GraphGuy
Meteorite | Level 14

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;

 

histogram_default.png

 

 

proc sgplot data=sashelp.heart;
where sex=:'F';
histogram agechddiag / nbins=7 fillattrs=(color=pink);
xaxis offsetmin=.2 offsetmax=.2;
run;

 

histogram_offset.png

 

View solution in original post

12 REPLIES 12
Anita_n
Pyrite | Level 9

Hi thanks, is binwidth=barwidth. This is not giving me the right results, even if I use even when I use 

binwidth= binstart=  showbins

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
Anita_n
Pyrite | Level 9

@PaigeMiller 


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;
PaigeMiller
Diamond | Level 26

Please explain what is wrong with the plot, and what you would like to see.

--
Paige Miller
Ksharp
Super User
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;

Ksharp
Super User


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;
Anita_n
Pyrite | Level 9

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

DanH_sas
SAS Super FREQ

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.

Anita_n
Pyrite | Level 9

Okay , thanks I now understand how it works

GraphGuy
Meteorite | Level 14

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;

 

histogram_default.png

 

 

proc sgplot data=sashelp.heart;
where sex=:'F';
histogram agechddiag / nbins=7 fillattrs=(color=pink);
xaxis offsetmin=.2 offsetmax=.2;
run;

 

histogram_offset.png

 

Anita_n
Pyrite | Level 9

@GraphGuy yes, that is exactly what I wanted. Thanks

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 12 replies
  • 4909 views
  • 5 likes
  • 5 in conversation