Mixed-Type Tables
Modify Tables
Tables enable you to use a single parameter to send mixed-type data into a SAS/IML module. The module can use the TableGetVarData function to extract values from the table into a matrix. It can also use the TableAddVar function to add new columns to the table.
The following module takes a table as an argument. It assumes that the table contains columns named Height and Weight, where height is measured in inches and weight is measured in pounds. (It could use the TableIsExistingVar function to verify that columns with these names exist.) The module extracts the data from these columns into vectors, and uses a standard formula to compute the body mass index (BMI) for each person in the data. The BMI values are then added to the table.
proc iml;
/* Compute BMI from the Height and Weight vars */
start AddBMI(tbl);
weight = TableGetVarData(tbl, "Weight");
height = TableGetVarData(tbl, "Height");
BMI = weight / height##2 * 703; /* standard formula */
call TableAddVar(tbl, "BMI", BMI); /* add numeric col */
return;
finish;
The following statements create a table from the Sashelp.Class data set. The table is passed into the AddBMI module, which modifies the table. When the module returns, the main program extracts the BMI values from the table and displays a histogram of the BMI values. The histogram is shown in Figure 5.
tClass = TableCreateFromDataSet("Sashelp", "Class");
run AddBMI(tClass); /* call a module that modifies the table */
bmi = TableGetVarData(tClass, "BMI");
call histogram(bmi); /* graph distribution of derived column */
Figure 5: Histogram of Computed Column
