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 FRACT*w*. 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.

## 9 Comments

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.

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

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

Pingback: The next power of 2 and other tricks with logarithms - The DO Loop

How input fraction 2/3 in SAS IML?

I assume you are asking about how to build a vector from expressions. Typical syntax is

x = 0 // 1/2 // 1/3;

or

x = (0:2)/3;

Pingback: Discover information about SAS formats... programatically - The DO Loop

how to convert interger to fraction??

I think I'd define a function in FCMP that formats the number as CATS(x, "/1") when x is an integer and as PUTC(x,"fract.") otherwise. You can then use that function to define a user=defined format. If you run into problems, ask your question at the SAS Support Communities.