Options for Printing a Matrix

1

A matrix is an array of numbers or character strings. When I print a matrix, I usually want to see only the data. However, sometimes it is helpful to add row or column headings that indicate the names of variables or labels for rows.

A simple example is count data for a cross-tabulation. For example, in my 2010 paper, Rediscovering SAS/IML Software: Modern Data Analysis for the Practicing Statistician, I present the following table as part of a discussion of Fisher's exact test:

How can you print a table like this in SAS/IML software?

The SAS/IML PRINT statement has four useful options that control the display of a matrix.

COLNAME=matrix
    specifies a character matrix used for column headings.
ROWNAME=matrix
    specifies a character matrix used for row headings.
LABEL=label
    specifies a label for the matrix. If this option is not specified, the name of the matrix is used as a label.
FORMAT=format
    specifies a valid SAS or user-defined format to use when printing matrix values.

You can also use these options by specifying their first letters: C=, R=, L=, and F=.

To use the options, enclose them in square brackets after the name of the matrix that you are printing. For example, the following statements print the table shown earlier:

proc iml;
Airports = {"ATL" "DFW" "IAH"};
Carriers ={"Northwest" "US Airways" "Mesa Airlines"};
L = "Late Flights by Carriers and Airports";
LateFlights = {6 9 4, 6 7 1, 7 0 1}; /** data matrix **/
print LateFlights[rowname=Carriers colname=Airports label=L];

The counts in each cell are based on a sampling of ten flights, so if you want to convert the data to percentages you can divide by 10 and print the data by using the PERCENT7.2 format, as follows:

PctLate = LateFlights / 10;
print PctLate[rowname=Carriers colname=Airports 
              label=L format=PERCENT7.2];

This information is also mentioned in Chapter 2 of my book, Statistical Programming with SAS/IML Software. Download the chapter for FREE from the book's Web site.

Share

About Author

Rick Wicklin

Distinguished Researcher in Computational Statistics

Rick Wicklin, PhD, is a distinguished researcher in computational statistics at SAS and is a principal developer of SAS/IML software. 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 Comment

  1. Pingback: An easy way to use numbers for column headers - The DO Loop

Leave A Reply

Back to Top