When I am computing with SAS/IML matrices and vectors, I often want to label the columns or rows so that I can better understand the data. The labels are called headers, and the COLNAME= and ROWNAME= options in the SAS/IML PRINT statement enable you to add headers for columns and rows, respectively. However, sometimes I want to use numerical values as headers, which is a problem because the COLNAME= and ROWNAME= options expect character strings.
Edit (Nov 2018): If you have SAS/IML 15.1 or later, you can use numerical values directly as column headers. Applying a format is no longer required!
The solution, of course, is to use a SAS format to convert numbers to strings. The SAS/IML language supports the CHAR function to convert a number into a string. By default, the CHAR function applies the BEST. format with a default field width (usually 12). You can specify a field width by using an optional argument. You can also specify the number of decimal places in the number, which is equivalent to specifying a w.d format.
For example, suppose you have data that indicates the results of 10 rolls of a single six-sided die. You want to print the values of the faces along with a count of the number of times that each face appeared. You can use the TABULATE function to count the frequency of each face, as follows:
proc iml; rolls = {4 6 4 6 5 2 4 3 3 6}; call tabulate(Vals, Counts, rolls); print Vals, Counts; |
The problem with this output is that the labels (the faces) are not attached to the counts. You can use the COLNAME= option on the print statement to label columns. The following statement uses the CHAR function to convert the numerical faces to strings:
print Counts[colname=(char(Vals))]; /* use face value to label counts */ |
Although the CHAR function is sufficient for most data, you might sometimes need to apply a special SAS format. For example, your labels might be dates or times. You can the PUTN function to apply an arbitrary format to a number. For example, the following data specifies US highway fatalities during the 2014 Independence Day weekend and uses the DATE7. format to convert dates (which are stored as numbers) into strings:
days = "03JUL2014"d:"06JUL2014"d; /* stored as 19907:19910 */ fatalities = {118 212 87 123}; print fatalities[colname=(putn(days,"Date7."))]; |
You can use any string for column headers, including strings that include spaces.
You can use the same technique for adding row headers. For example, if you wanted to enumerate the rows of a matrix that has N rows, you can specify colname=(char(1:N)) as an option to the PRINT statement.
1 Comment
Pingback: Use numeric values for column headers when printing a matrix - The DO Loop