BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
A_Kh
Barite | Level 11

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? 

 

1 ACCEPTED SOLUTION

Accepted Solutions
DanH_sas
SAS Super FREQ

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;

View solution in original post

13 REPLIES 13
DanH_sas
SAS Super FREQ

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;
A_Kh
Barite | Level 11

Hi @DanH_sas
Thank you for the feedback!
This might be the very last option I could do if histograms not possible. 

DanH_sas
SAS Super FREQ

For your #1 example above, what do you get if you also set BINWIDTH=1?

A_Kh
Barite | Level 11

The same result with an additional NOTE:

NOTE: The specified BINWIDTH= value will be ignored in order to accommodate the data
DanH_sas
SAS Super FREQ

Try dropping the NBINS and see if the BINWIDTH is honored.

A_Kh
Barite | Level 11

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;  

ex1.PNG

Ksharp
Super User

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;  

Ksharp_0-1743471225548.png

 

A_Kh
Barite | Level 11

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;

ex2.PNG

out 

Ksharp
Super User
I don't see any difference between VBAR and HISTOGRAM. Can you post a desired graph you need ?
A_Kh
Barite | Level 11

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

Tom
Super User Tom
Super User

@A_Kh wrote:

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


I doubt it since the graph you want isn't actually a histogram.

A_Kh
Barite | Level 11

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. 

Tom
Super User Tom
Super User

Oxford Dictionary definition of histogram.

Tom_0-1743623494677.png

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 13 replies
  • 1706 views
  • 4 likes
  • 4 in conversation
OSZAR »