I'm trying to create a histogram with proc sgplot but SAS will only create a single bar histogram plot.
My data is continuous, highly skewed, with 10,677 zero values. When I run tests on simulated lognormal data, proc sgplot does fine.
Perhaps something about my data is causing issues?
proc means data=out.cohort_match_real_data_pos n mean median stddev min max;
where ever_eng_fst12_month="No";
var pre_post_cost_712_mon_ratio;
run;
/* N Mean Median Std Dev Minimum Maximum */
/* ----------------------------------------------------------------------------------------*/
/* 37670 8.9096483 0.4685912 117.5225125 0 15089.56 */
/* ----------------------------------------------------------------------------------------*/
proc sgplot data=out.cohort_match_real_data_pos;
where ever_eng_fst12_month="No";
histogram pre_post_cost_712_mon_ratio / nbins=40 binwidth=.5 binstart=0;
/* density pre_post_cost_712_mon_ratio /type=normal;*/
xaxis values=(0 to 20 by .5);
run;
It doesn't have to be ERROR message, the answer is right here if you would just read the log: (Maxim 2, Read the Log)
NOTE: The specified BINWIDTH= value will be ignored in order to accommodate the data.
So SAS chooses its own binwidth, which apparently is very wide. If you only want data between 0 and 20 to appear in the histogram, eliminate any values >20
Are there messages in the log that might indicate what the problem is?
From now on, when code doesn't work, show us the log (all of it for this PROC, not just the ERRORs or WARNINGs)
It doesn't have to be ERROR message, the answer is right here if you would just read the log: (Maxim 2, Read the Log)
NOTE: The specified BINWIDTH= value will be ignored in order to accommodate the data.
So SAS chooses its own binwidth, which apparently is very wide. If you only want data between 0 and 20 to appear in the histogram, eliminate any values >20
That worked!
proc sgplot data=out.cohort_match_real_data_pos;
where ever_eng_fst12_month="No" and pre_post_cost_712_mon_ratio <= 20;
histogram pre_post_cost_712_mon_ratio / nbins=40 binwidth=.5 binstart=0;
/* density pre_post_cost_712_mon_ratio /type=normal;*/
xaxis values=(0 to 20 by .5);
run;
Follow up question:
Is there a way to change my x-axis in proc sgplot without changing the percentages for each bin? I understand SAS is recalculating the %'s when I subset the population, which it's supposed to do.
But if I want to "zoom in" on a portion of the x-axis without renormalizing the %'s, is there a handy option in sgplot? Or better to manually calculate the bins and %'s, say with proc freq & output a bin_freq dataset, and then run sgplot on bin_freq.
Your maximum value is 15089 but your X axis only goes to 20?
@RobertWF1 wrote:
I'm trying to create a histogram with proc sgplot but SAS will only create a single bar histogram plot.
My data is continuous, highly skewed, with 10,677 zero values. When I run tests on simulated lognormal data, proc sgplot does fine.
Perhaps something about my data is causing issues?
proc means data=out.cohort_match_real_data_pos n mean median stddev min max; where ever_eng_fst12_month="No"; var pre_post_cost_712_mon_ratio; run; /* N Mean Median Std Dev Minimum Maximum */ /* ----------------------------------------------------------------------------------------*/ /* 37670 8.9096483 0.4685912 117.5225125 0 15089.56 */ /* ----------------------------------------------------------------------------------------*/ proc sgplot data=out.cohort_match_real_data_pos; where ever_eng_fst12_month="No"; histogram pre_post_cost_712_mon_ratio / nbins=40 binwidth=.5 binstart=0; /* density pre_post_cost_712_mon_ratio /type=normal;*/ xaxis values=(0 to 20 by .5); run;
Run the code without any options and add them back in one by one to see where the issue is.
proc sgplot data=out.cohort_match_real_data_pos;
where ever_eng_fst12_month="No";
histogram pre_post_cost_712_mon_ratio ;
run;
If I run sgplot with no options, I get the following default plot (same with proc univariate histogram).
It's lumping most of my observations into one category.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.