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.

Share Distinguished Researcher in Computational Statistics

Rick Wicklin, PhD, is a distinguished researcher in computational statistics at SAS and is a principal developer of PROC IML and SAS/IML Studio. His areas of expertise include computational statistics, simulation, statistical graphics, and modern methods in statistical data analysis. Rick is author of the books Statistical Programming with SAS/IML Software and Simulating Data with SAS.

1. 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.

2. 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;

3. 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.

4. Pingback: The Farey sequence - The DO Loop