Hello all,
I'm using proc univariate to create a histogram of my data, anyone knows how to output the midpoints of each bin and the corresponding percentage?
The code I use is:
proc univariate data=have; where Prep>=0.5 and Prep<130.5; histogram Prep / endpoints=(0.5 to 130 by 5); run;
By using this code, I could get the histogram, with my own defined endpoint and interval. But I don't know how to output a table (dataset) including the midpoints (or endpoints) and its percentage.
Proc freq can produce the percentage of every value in my data, but not for the bins.
Thanks!
Nope, but it does appear the documentation may be wrong. This is why I recommend the ODS TRACE options instead - run the proc with ODS TRACE ON; <procedure code> ;; ODS TRACE OFF; Then capture the name from the log.
Try Histogram instead, this works for me - SAS 9.4 TS1M4
ods table histogram=want;;
proc univariate data=sashelp.cars;
histogram mpg_highway / endpoints=(0 to 80 by 5);;
run;
Have you tried capturing the ODS Table?
https://blogs.sas.com/content/iml/2017/01/09/ods-output-any-statistic.html
Did you try it and it didn't work, or did you make that decision before trying it?
@hua wrote:
Hi Reeza, I read the link, but I don't think ODS can help to output the midpoints and percentage, because there has no options for the table I want. Or could you tell me more details about it? Thank you so much!
The output wasn't named HistogramBins in the ODS TRACE step...
Hi Reeza, this is what I found, I think the table name is HistogramBins, did I misunderstand this option?
Nope, but it does appear the documentation may be wrong. This is why I recommend the ODS TRACE options instead - run the proc with ODS TRACE ON; <procedure code> ;; ODS TRACE OFF; Then capture the name from the log.
Try Histogram instead, this works for me - SAS 9.4 TS1M4
ods table histogram=want;;
proc univariate data=sashelp.cars;
histogram mpg_highway / endpoints=(0 to 80 by 5);;
run;
@hua wrote:
Hi Reeza, this is what I found, I think the table name is HistogramBins, did I misunderstand this option?
You may have missed the part that the Bins or HistogramBins are only created if you use MIDPERCENTS option on the HIstogram statement.
@ballardw I checked that, still doesn't work. It's a mistake in the documentation.
@ballardw wrote:
@Reeza wrote:
@ballardw I checked that, still doesn't work. It's a mistake in the documentation.
ARGGH! I tend to believe the documentation for features I don't use...
I double checked, I was wrong and the documentation is sort of right.
1. It's MIDPERCENTS - not MIDPOINTS which I had tested. When you use MIDPERCENTS it does produce a table called HistogramBins. Otherwise the table is called Histogram. Which isn't listed in the list.
The inconsistency still doesn't make sense.
The easiest way to do this is to use the OUTHIST= option on the HISTOGRAM statement:
proc univariate data=sashelp.cars;
var mpg_city;
histogram mpg_city / midpoints=(5 to 65 by 10)outhist=want;
run;
proc print data=want; run;
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!
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.