Data visualization with SAS programming

Create histogram for values upto 10 decimal points

Reply
Frequent Contributor
Posts: 124

Create histogram for values upto 10 decimal points

Hi All,

I created a histogram which has values for variable 'xyz' up to the 9th or  10th decimal point.  I have copied the code below. The  x-axis shows values up to the 4th decimal point (which is what I want in the display as it looks presentable). But, I think the code also approximates the values to the 4th decimal point to create the bars. In certain examples , the values of 'xyz' are so similar to the 4th decimal point that it just gives me a single bar. I am new to SAS/GRAPH. Could you suggest what options  I could use to create graphs for values upto the 10th decimal point but display them on x-axis upto the 4th decimal point. Please advice. Thanks !
This is my code :

It's a generic code that seeks the minimum and maximum value of the field field i.e for xyz, the minimum value is represented my the macro &xyzmin and the maximum value by &xyzmax

ods graphics on / reset=index imagename = "image_histogram.”;

proc sgplot data=outcome;

histogram estxyz/binstart=&xyzmin.

                   binwidth=0.5;

xaxis values=(&xyzmin. to &xyzmax. by 2)

                   valueshint;

run;


Here is the data for 'xyz' that displays a single bar in the histogram. Please ignore the observation numbers in the table below. Also I have 10,000 such observations.


xyz

  1. 2.053560486
  2. 2.05358304
  3. 2.053586672
  4. 2.053595518
  5. 2.053620218
  6. 2.053622878
  7. 2.053629565
  8. 2.053634162
  9. 2.053636113
  10. 2.053640661
  11. 2.053642102
  12. 2.053652491
  13. 2.053655273
  14. 2.053658328
  15. 2.053664305
  16. 2.053666429
  17. 2.053668664
  18. 2.053673135
  19. 2.053673347
  20. 2.053680921
  21. 2.053681324

Here is the graph I get based on the present code

Super User
Posts: 11,104

Re: Create histogram for values upto 10 decimal points

I think your setting of BINWIDTH is involved. Setting it to .5 says the largest-smallest value within a bin will be .5. ALL of the data you show are within .5.

Try using NBINS to specify the number of bins to create. I would also wait to add the XAXIS statement until after I get a workable graph.

SAS Super FREQ
Posts: 1,134

Re: Create histogram for values upto 10 decimal points

BallardDW is right.  This may have to do with your settings.  Using SAS 9.4M2, here is what I get with just the "histogram xyz" statement.  Note - You will get an interval x-axis by default, not a bin axis.

proc sgplot data=hist;

  histogram xyz;

  run;

Histogram1.png

Frequent Contributor
Posts: 124

Re: Create histogram for values upto 10 decimal points

Thanks for your replies. @ballardw, you are right, the bin width specification does affect the display. So I took it out and just like you @Sanjay@SAS I got a graph that's better then the first. I have pasted it below. However, I think its pretty broad and although it gives information on the distribution , could it get more detailed i.e more number of bins? I think thats the reason I had chosen to specify the bin width before. Also, how is it @Sanja@SAS I am not getting the x-axis display such as yours ? Could you please suggest based on my initital code where I wrote . As you know , I need to have a generic code. Thanks.

xaxis values=(&xyzmin. to &xyzmax. by 2)

                   valueshint;

Frequent Contributor
Posts: 124

Re: Create histogram for values upto 10 decimal points

I missed your comment on the defualt x-axis. Let me remove the

xaxis values=(&xyzmin. to &xyzmax. by 2) valueshint ; and see what I get. Nevertheless, i still would like the graph to more bin detailed without compromising on the values. That obviously means I have to specify the 'bin number' and since the input data changes I will need to have something generic. Any suggestions on it would be very helpful !

Frequent Contributor
Posts: 124

Re: Create histogram for values upto 10 decimal points

All right. I do get a better display on the x-axis after removing the specifications on the x-axis on the code. For now this works. Thanks much !

Frequent Contributor
Posts: 124

Re: Create histogram for values upto 10 decimal points

My browser is not showing me the buttons to mark the answers as correct or helpful. I will definitely get back to it later.

Super User
Posts: 11,104

Re: Create histogram for values upto 10 decimal points

The original post was not created as a question and discussions don't show the mark buttons. No problem, we'll consider them marked.

Frequent Contributor
Posts: 124

Re: Create histogram for values upto 10 decimal points

Thanks !

Frequent Contributor
Posts: 124

Re: Create histogram for values upto 10 decimal points

@ballardw and @Sanjay. I have one lingering question. After I removed the x-axis specifications , although I get values on the x-axis it doesn't display the minimum and maximum value. Is it possible to display that ? Thanks.

SAS Super FREQ
Posts: 925

Re: Create histogram for values upto 10 decimal points

Set THRESHOLDMIN=1 and THRESHOLDMAX=1 on the XAXIS statement.

SAS Super FREQ
Posts: 1,134

Re: Create histogram for values upto 10 decimal points

THRESHOLDMIN and MAX will show you the outer tick, but not the max or min value of your data.  You have a couple of options.  Add a fringe plot.  This will show you all the observations.:

ods graphics / reset imagename='Histogram';

proc sgplot data=hist;

  histogram xyz;

  fringe xyz;

  run;

Histogram.png

Now, if you want the lowest and highest value labeled, you could copy the lowest and highest values to another column called Extreme, with other rows missing.  Then, use a drop line to plot the extreme column with data label.

Frequent Contributor
Posts: 124

Re: Create histogram for values upto 10 decimal points

Thanks DanH@sas and Sanjay@SAS. I am going to try both the suggestions and see what I get. Also, Sanjay@SAS I am not sure why you are getting multiple tick marks within the bars . I am guessing its because of the data ? Is there any particular significance for it ?

SAS Super FREQ
Posts: 1,134

Re: Create histogram for values upto 10 decimal points

The FRINGE plot shows the location of each observation in the data.  Many users like to see the shape of the histogram and also the locations of each observation.  Now, there are ways to place labels of the min and max observations to show their value, but it takes some extra work, and I am not sure how much trouble you are willing to go to get this.  Note:  I increased the format size for the extreme labels.

HistogramExtreme.png

Frequent Contributor
Posts: 124

Re: Create histogram for values upto 10 decimal points

Hi I tried the x-axis options for the display of min and max values and I do get a graph (copy-pasted below)

However, a .exe window opens with a message i.e.

‘-axis’ is not recognized as an internal or external command, operable program or batch file


I think there is an error in my syntax. Please advice. I want to avoid the .exe window.


proc sgplot data=outcome;

histogram xyz;

x-axis thresholdmin=1 and thresholdmax=1;

run;

@ I really liked the 'fringe' functionality and  tried it. Since I have about 10,000 records with values close to each other, I get a x-axis which gets very crowded with tick marks. That doesn't make it a very informative graph for me (copy-pasted below). Also, I am eager to know how to place labels for the min and max value

Graph using thresholdmin and thresholdmax options :

Graph using fringe statement:

Ask a Question
Discussion stats
  • 16 replies
  • 855 views
  • 3 likes
  • 4 in conversation