- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello Community,
I'm having difficulty in creating a bin for each category of the histogram. The problem is one of the response categories has relatively small frequency and when using the sgplot procedure I couldn't create a bin for that category. The SAS Documentation I could find was not helpful enough to solve this issue. I would appreciate your support.
Below is an example using sashelp.cars data.
Trying 2 different ways:
1. Doesn't create a bin and tick value for all required categories (4, 5, 6, 8), instead creates a bin for non-existing value (10).
proc freq data=sashelp.cars;
where cylinders between 4 and 8;
table cylinders;
run;
proc sgplot data= sashelp.cars;
where cylinders between 4 and 8;
histogram cylinders/nbins=4 binstart=4 showbins dataskin=pressed;
yaxis label='Percent' ;
xaxis label='Cylinder' ;
run;
2. Creates tick values for required categories (4, 5, 6, 8), but not creating a bin for category 5 with a centered tick value.
proc sgplot data=sashelp.cars;
where cylinders between 4 and 8;
histogram cylinders/nbins=4 binstart=4 showbins dataskin=pressed;
yaxis label='Percent';
xaxis label='Cylinder' values=(4 5 6 8) offsetmin=0.2 offsetmax=0.2;
run;
How to fix it in either way, that is to create 4 bins for categories 4, 5, 6, 8 and display tick values in the center of the bin?
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Would a bar chart be sufficient for this case? Try this and see if you get what you want:
proc sgplot data=sashelp.cars;
where cylinders between 4 and 8;
vbar cylinders / dataskin=pressed stat=pct;
yaxis label='Percent';
xaxis label='Cylinder';
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Would a bar chart be sufficient for this case? Try this and see if you get what you want:
proc sgplot data=sashelp.cars;
where cylinders between 4 and 8;
vbar cylinders / dataskin=pressed stat=pct;
yaxis label='Percent';
xaxis label='Cylinder';
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi @DanH_sas,
Thank you for the feedback!
This might be the very last option I could do if histograms not possible.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
For your #1 example above, what do you get if you also set BINWIDTH=1?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The same result with an additional NOTE:
NOTE: The specified BINWIDTH= value will be ignored in order to accommodate the data
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Try dropping the NBINS and see if the BINWIDTH is honored.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes, it does. However, now a new bin gets added for category 7.
*Ex1;
proc sgplot data= sashelp.cars;
where cylinders between 4 and 8;
histogram cylinders/binwidth=1 binstart=4 showbins dataskin=pressed;
yaxis label='Percent' ;
xaxis label='Cylinder' ;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I can not understand your question well .
Can you post your desired output/graph ?
And that is not a HISTOGRAM graph, it is just a VBAR graph as DanH_sas showed you .
proc freq data=sashelp.cars noprint;
where cylinders between 4 and 8;
table cylinders/out=temp;
run;
data temp;
set temp;
y=percent/2;
run;
proc sgplot data= temp noautolegend;
vbarparm category=cylinders response=percent;
text x=cylinders y=y text=cylinders/strip contributeoffset=none textattrs=(size=12);
yaxis label='Percent' ;
xaxis label='Cylinder' display=none;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi @Ksharp ,
Thank you for the feedback. The result I need is almost the same as @DanH_sas and you showed using VBAR/VBARPARAM statements, only without a space between bars. Just adding BARWIDTH=1 option eliminates the space and makes bars look like histogram. I was wondering if I can get the same result using HISTOGRAM statement.
*Ex2;
proc sgplot data=sashelp.cars;
where cylinders between 4 and 8;
vbar cylinders / barwidth=1 dataskin=pressed stat=pct;
yaxis label='Percent';
xaxis label='Cylinder';
run;
out
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi @Ksharp ,
Yes, you are right. Sorry for being unclear.
My last post showed the desired graph using VBAR statement.
I wondered if the same result could be achieved with HISTOGRAM statement...
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Looks like I need to go with Bar chart when visualizing categorical data. As data are mostly continuous variables, the requirement was histogram graphs. I don't really see the difference between my last graph(bar chart without a gap between bars) and histogram. As you said this is not a histogram then there should be some differences. Please let me know if there is a visual distinction between the last graph and a histogram, except the type of data they should use.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Oxford Dictionary definition of histogram.
Your graph using the HISTOGRAM statement had a zero height bar for the interval centered on 7 because the frequency of that class interval was zero.