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

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;

 

RobertWF1_1-1641317459427.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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

--
Paige Miller

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

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)

--
Paige Miller
RobertWF1
Quartz | Level 8
Here's the log - no errors:

23 GOPTIONS ACCESSIBLE;
24 proc sgplot data=out.cohort_match_real_data_pos;
25 where ever_eng_fst12_month="No";
26 histogram pre_post_cost_712_mon_ratio / nbins=40 binwidth=.5 binstart=0;
27 /* density pre_post_cost_712_mon_ratio /type=normal;*/
28 xaxis values=(0 to 20 by .5);
29 run;

NOTE: PROCEDURE SGPLOT used (Total process time):
real time 7.51 seconds
cpu time 5.06 seconds

NOTE: The specified BINWIDTH= value will be ignored in order to accommodate the data.
NOTE: Some of the tick values have been thinned.
NOTE: Listing image output written to D:\SASWORK\rfeyerharm\_TD3252_VMSAS01_\Prc2\SGPlot38.png.
NOTE: Some of the tick values have been thinned.
NOTE: There were 41355 observations read from the data set OUT.COHORT_MATCH_REAL_DATA_POS.
WHERE ever_eng_fst12_month='No';
PaigeMiller
Diamond | Level 26

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

--
Paige Miller
RobertWF1
Quartz | Level 8

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;

 

RobertWF1_0-1641319720005.png

 

RobertWF1
Quartz | Level 8

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.

 

Reeza
Super User

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;

 

RobertWF1_1-1641317459427.png

 


 

 

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;
RobertWF1
Quartz | Level 8

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.

 

RobertWF1_0-1641319285570.png

 

 

Reeza
Super User
Yes, because a large proportion are zeroes as indicated.
Why not exclude those to see the distribution of the remainder?

where ever_eng_fst12_month="No" and pre_post_cost_712_mon_ratio > 0 ;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

Creating Custom Steps in SAS Studio

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 8 replies
  • 2279 views
  • 4 likes
  • 3 in conversation