Data visualization with SAS programming

Apply a format to x-axis of a histogram using proc univariate

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 121
Accepted Solution

Apply a format to x-axis of a histogram using proc univariate


Hi ,

I created a histogram on a numeric variable which has 10 digts after the decimal point. I want that to be retained as I like the distribution of the values by the bars, but the numbers displayed on the x-axis should have only 2 digits after the decimal point. Is it possible to manipulate the format of the x -axis in proc univariate only ? My code is:

proc univariate data=test;

  var var1;

histograms /midpoints= 5.6703546745 to 35.9034765215 by 0.5;  /* assume the minimum value of var1 is 5.6703546745 and maximum value of var1 is 35.9034765215 */

run;

Thanks.


Accepted Solutions
Solution
‎03-24-2015 12:33 PM
SAS Super FREQ
Posts: 1,081

Re: Apply a format to x-axis of a histogram using proc univariate

Can you attach a sample data set that will work with your program?  Then we can try ways ways to improve the graph? 

Do you care if the X axis has a simple linear tick values?  That usually works well in such cases, unless you absolutely need these long bin edge values on the x axis.  You can get that by using the Histogram statement in proc SGPLOT instead of proc UNIVARIATE.

View solution in original post


All Replies
Grand Advisor
Posts: 10,211

Re: Apply a format to x-axis of a histogram using proc univariate

I would try adding a statement: Format var1 f5.2;

By default my midpoints default to 2 decimals with data in a similar range.

Frequent Contributor
Posts: 121

Re: Apply a format to x-axis of a histogram using proc univariate

@ballardw...I don't want to change the format of var1 , because once you round up all the values they are very similar and I don't  get get a detailed graph. I just want to change the display of the major unit on the x-axis and round those values to the 2nd decimal point. I was looking through all options in proc univariate and I didn't come across any for the x- or horizontal axis as yet. Do you have any suggestions for me ? or is it just not possible with proc univariate ?

SAS Super FREQ
Posts: 1,081

Re: Apply a format to x-axis of a histogram using proc univariate

It is hard to see what is going on and make suggestions if you do not provide the full program with data.  Also provide the SAS release you are using.

Frequent Contributor
Posts: 121

Re: Apply a format to x-axis of a histogram using proc univariate

@Sanjay@SAS..I understand.  I am working on it and will post it soon.

Frequent Contributor
Posts: 121

Re: Apply a format to x-axis of a histogram using proc univariate

@ballardw and @Sanjay@SAS.....

I am using SAS 9.4. My present graph's x-axis looks like this. This is just an example. My original graph shows a nice bell shaped distribution as it has a lot of observations and the right intervals on the x-axis. Now ,I am only interested in displaying 2 digits after the decimal point on major unit on the horizontal axis . The reason being I have 1000 such observations and it gets too crowded on the x-axis. I do not wish to change the format of the var1 variable and round it to the 2nd decimal , because I then loose the details in the distribution of the data.

My code is generic as the input data set changes but the variable name is the same.

proc univariate data=a.&dataset. ;

  var var1;

     histogram/ midpoints=&var1_min to &var1_max by 0.5

              rtinclude;

run;

Solution
‎03-24-2015 12:33 PM
SAS Super FREQ
Posts: 1,081

Re: Apply a format to x-axis of a histogram using proc univariate

Can you attach a sample data set that will work with your program?  Then we can try ways ways to improve the graph? 

Do you care if the X axis has a simple linear tick values?  That usually works well in such cases, unless you absolutely need these long bin edge values on the x axis.  You can get that by using the Histogram statement in proc SGPLOT instead of proc UNIVARIATE.

Grand Advisor
Posts: 10,211

Re: Apply a format to x-axis of a histogram using proc univariate

If I understand that

Format var1 f4.2; doesn't work then you have much more work in defining boundaries and then using a different procedure to plot the data using a different procedure.

You might be able to create a custom format from you macro variables such that you set the range and have the value displayed to the precision you want.

Frequent Contributor
Posts: 121

Re: Apply a format to x-axis of a histogram using proc univariate

I am trying to figure the same @ballardw but would like a simpler solution. @Sanjay@SAS and ballardw here is a better example of my graph and closely represents my original graph. Now since there are 100 observations on the x-axis...major unit values are represented upto 4 digits after the decimal point. Would doing this graph in sgplot give me better control on the x-axis major unit format? @Sanjay@SAS , this is the range of values in var1. Hope this helps. Thanks.

SAS Super FREQ
Posts: 890

Re: Apply a format to x-axis of a histogram using proc univariate

As said, I would try using PROC SGPLOT. Use the BINSTART and BINWIDTH options on the HISTOGRAM statement to set up the midpoints as you did with UNIVARIATE. By default, the x-axis will display using a linear style axis. There is a BINAXIS option; but I agree with Sanjay that the linear axis will probably look a lot better.

Frequent Contributor
Posts: 121

Re: Apply a format to x-axis of a histogram using proc univariate

All right I will try proc sgplot, but before I do that...what do you mean by linear style axis ? Does it mean the major unit values will rounded up to a whole number ? I do need it by the 2nd decimal point though. If I remember, I shall be able to achieve that using sgplot.

SAS Super FREQ
Posts: 890

Re: Apply a format to x-axis of a histogram using proc univariate

You can use the VALUES option on the XAXIS statement to specify the first, last and increment of the tick values you want. Also, specify the VALUESHINT so that the data range of the axis is not affected. Neither of these settings should affect the histogram midpoints.

SAS Super FREQ
Posts: 1,081

Re: Apply a format to x-axis of a histogram using proc univariate

By default, the axis tick values are placed at "rounded" values in the correct scaled location and marked as such.  So, for your case, the x axis tick values will likely be 4, 6, 8, 10 and so on, placed at the right location on the x axis.  You will only see the number of decimals that are needed.

The tick values will NOT necessarily line up with the bins.  See example below.  The first is UNIVARIATE, each bin has a value at its center.  The second is SGPLOT, where the x axis has rounded values evenly placed at the right location, but not necessarily at the center of each bin. The second can be useful with a large number of bins.

You can specify other tick values as you need as suggested by Dan.

Hist_Univariate.png

Hist_SGPlot.png

Frequent Contributor
Posts: 121

Re: Apply a format to x-axis of a histogram using proc univariate

Thank you Sanjay@SAS and DanH for your advice. I will get working on it now.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 13 replies
  • 2298 views
  • 9 likes
  • 4 in conversation