How to format decimals as fractions in SAS

Yesterday I blogged about the Hilbert matrix. The (i,j)th element of the Hilbert matrix has the value 1 / (i+j-1), which is the reciprocal of an integer.

However, the printed Hilbert matrix did not look exactly like the formula because the elements print as finite-precision decimals. For example, the last column of the matrix of size 5 is {0.2, 0.1666667, 0.1428571, 0.125, 0.1111111}. A colleague jokingly asked, "shouldn't the matrix contain fractions like 1/5, 1/6, 1/7, 1/8, and 1/9?"

To his surprise, I responded that SAS can actually print the matrix elements as fractions! SAS contains the FRACTw. format, which makes it easy to print decimals as their fractional equivalent in reduced form. Here is yesterday's matrix, printed as fractions:

print H[format=FRACT.];

I sometimes marvel at the variety of formats that are available in SAS software. From printing integers as Roman numerals to printing decimals as fractions, it seems like SAS has a format for all occasions.

What is your favorite SAS format? Why? Leave a comment.

tags: Just for Fun

3 Comments

  1. Scott K
    Posted April 11, 2014 at 11:43 am | Permalink

    I have always found the Zw.d format useful for its ability to preserve leading zeros in data. Thank you for introducing me to the FRACTw. format--I have not seen it until now.

    • Posted April 11, 2014 at 11:54 am | Permalink

      I love the Zw.d format. Very useful for displaying zip codes in New England (such as Boston 02128) and for aligning patient IDs.

  2. Posted April 14, 2014 at 9:23 am | Permalink

    Very cool, Rick. As usual, my first thought was: can R do that? It can. See our post at http://sas-and-r.blogspot.com/2014/04/example-20144-hilbert-matrix.html

One Trackback

  1. […] Notice that the FRACTw.d format enables you to format decimals as fractions in SAS. […]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <p> <pre lang="" line="" escaped=""> <q cite=""> <strike> <strong>