Print tables in SAS/IML
source link: https://blogs.sas.com/content/iml/2017/04/03/print-tables-sasiml.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
One of the advantages of the new mixed-type tables in SAS/IML 14.2 (released with SAS 9.4m4) is the greatly enhanced printing functionality. You can control which rows and columns are printed, specify formats for individual columns, and even use templates to completely customize how tables are printed. Printing a table is accomplished by using the TablePrint subroutine, which has many more options than the well-known SAS/IML PRINT statement for matrices.
The TablePrint subroutine
The TablePrint subroutine provides many options that give you control over the output:
- The FIRSTOBS= and NUMOBS= options specify the range of observations to print.
- The ID= option specifies a variable to use for the row header on the left side of the table.
- The VAR= option specifies the columns (and the column order) for the table.
- The LABEL= option specifies a spanning header for the table.
For example, the following DATA step creates random birthdays for the students in the Sashelp.Class data set. The Birthday variable is formatted with the DATE9. format. The data is then read into a SAS/IML table by using the TableCreateFromDataSet function. Finally, the TablePrint subroutine prints a customize portion of the table:
data class; set sashelp.class; Birthday = '03APR2017'd - age*365 - floor(365*uniform(1)); /* create birthday */ format Birthday DATE9.; run; proc iml; tClass = TableCreateFromDataSet("Class"); /* read data into table */ run TablePrint(tClass) firstobs=3 numobs=5 var={"Age" "Birthday"} ID="Name" label="Subset of Class";
Notice that the table contains both numeric and character columns. Furthermore, the numeric columns have different formats. The TablePrint subroutine has some distinct advantages over the traditional PRINT statement in SAS/IML:
- The TablePrint subroutine supports an easy way to display a range of observations. When you use the PRINT statement for multiple vectors, you have to use row subscripts in each vector, such as PRINT (X[3:8,]) (Y[3:8,]);
- The TablePrint subroutine supports printing any columns in any order. When you use the PRINT statement on a matrix, you have to use column subscripts to change the order of the matrix columns: PRINT (X[, {2 3 1}]);
- The PRINT statement supports the ROWNAME= option (for specifying row headers), the COLNAME= option (for specifying column headers), and the LABEL= option. Those options are easy to work with when you print a single matrix. However, you can't store mixed-type data in a matrix and those options are less convenient when you print a set of vectors.
Advanced printing of SAS/IML tables
The SAS/IML documentation has several sections of documentation devoted to advanced printing of SAS/IML tables. For example, you can use the TablePrint subroutine to do the following:
- Use a custom template to display a table.
- Specify values for dynamic variables in templates.
- Use an existing ODS template to format and display data that are in SAS/IML.
The documentation contains an example of "traffic lighting," where the colors of cells depend on the value in the cells, as shown to the right.
For statistical programmers, the ability to use ODS templates means that output from PROC IML can look the same as output from some other SAS procedue. For example, suppose that you have a table that contains parameter estimates for a linear regression. The following example prints that table by using the same ODS template that PROC REG uses, which is the Stat.Reg.ParameterEstimates template:
proc iml; vars = {"Intercept", X, Z}; stats = {32.19 5.08 21.42 42.97, 0.138 0.0348 0.0644 0.2117, 1.227 0.5302 0.1027 2.3506 }; tbl = TableCreate("Variable", vars); call TableAddVar(tbl, {"Estimate" "StdErr" "LowerCL" "UpperCL"}, stats); Confidence=95; call TablePrint(tbl) template="Stat.Reg.ParameterEstimates" dynamic={Confidence};
This example works because the column names in the SAS/IML table match the names that are expected by the Stat.REG.ParameterEstimates template. The DYNAMIC= option specifies a dynamic variable (Confidence) that the template requires. See the documentation for further details.
Summary
In summary, the TablePrint subroutine in SAS/IML gives programmers control over many options for printing tables of data and statistics. For complex layouts, you can use an existing ODS template or create your own template to customize virtually every aspect of your tabular displays. For more information about SAS/IML tables, see the SAS Global Forum paper "More Than Matrices" (Wicklin, 2017).
Recommend
-
8
Define functions with optional parameters in SAS/IML 1 ...
-
2
Use lists to pass parameters to SAS/IML functions 0 A popular way to...
-
6
Lists are collections of objects. SAS/IML 14.2 supports lists as a way to store matrices, data tables, and other lists in a single object that you can pass to functions. SAS/IML lists automatically grow if you add new items to them and shri...
-
8
On passing a list to a SAS/IML module 0 SAS/IML programmers often create...
-
11
Rolling statistics in SAS/IML 1 L...
-
11
SAS/IML 14.3 (SAS 9.4M5) introduced a new syntax for creating lists and for assigning and extracting item in a list. Lists (introduced in SAS/IML 14.2) are data structures that are convenient for holding heterogeneous data. A single list can...
-
9
To get better at something, you need to practice. That maxim applies to sports, music, and programming. If you want to be a better programmer, you need to write many programs. This article provides an example of forming the intersection of i...
-
4
Create discrete heat maps in SAS/IML 7 In a previous article I introduc...
-
4
For programmers who are learning the SAS/IML language, it is sometimes confusing that there are two kinds of multiplication operators, whereas in the SAS DATA step there is only scalar multiplication. This article describes the multiplication ope...
-
2
Write to the log from SAS IML programs » SAS博客列表 I previously discussed how to...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK