MODELMATRIX Procedure

Getting Started: MODELMATRIX Procedure

(View the complete code for this example.)

The Sashelp.Baseball data set contains salary and performance information for Major League Baseball players, excluding pitchers, who played at least one game in both the 1986 and 1987 seasons. The salaries (Sports Illustrated, April 20, 1987) are from the 1987 season, and the performance measures are from 1986 (Collier Books, The 1987 Baseball Encyclopedia Update). The following step displays (in Figure 1) the variables in the data set that are used in this example:

proc contents varnum data=sashelp.baseball
(KEEP = League Division nHits yrMajor);
   ods select position;
run;

Figure 1: Sashelp.Baseball Data Set

The CONTENTS Procedure

Variables in Creation Order
#VariableTypeLenLabel
4nHitsNum8Hits in 1986
9YrMajorNum8Years in the Major Leagues
16LeagueChar8League at the End of 1986
17DivisionChar8Division at the End of 1986


Assume that you want to predict the logSalary by using the categorical variables League and Division and the interaction of the nHits and YrMajor variables. The goal is to create a design matrix that represents this regression model. This example shows how you can use PROC MODELMATRIX for this purpose. Because the variation in salaries is much greater for the higher salaries, a log transformation is first applied to the salaries.

Note: Input data must be in a CAS table that is accessible in your CAS session. You must refer to this table by using a two-level name. The first level must be a CAS engine libref, and the second level must be the table name. For more information, see the sections Using CAS Sessions and CAS Engine Librefs and Loading a SAS Data Set onto a CAS Server in Chapter 2, Shared Concepts.

You can load the Sashelp.Baseball data set by using the following DATA step:

data mylib.baseball;
   set sashelp.baseball;
   index = _N_;
run;

These statements assume that your libref is named mylib, as in the section Using CAS Sessions and CAS Engine Librefs, but you can substitute any appropriately defined libref.

The following statements define the described model, create the design matrix, and store the design matrix in the output table mylib.designMat:

proc modelmatrix data=mylib.baseball;
  class League Division;
  model logSalary = League Division nHits*yrMajor;
  output out = mylib.designMat copyvar=index;
run;

The output from this analysis is presented in Figure 2 through Figure 4.

Figure 2: Number of Observations, Dimensions, and Column Names of the Design Matrix

The MODELMATRIX Procedure

Number of Observations Read322
Number of Observations Used263

Dimensions
Number of Effects4
Number of Parameters6

Column Names of the Design
Matrix
NameParameter
Col1Intercept
Col2League American
Col3League National
Col4Division East
Col5Division West
Col6nHits * YrMajor


Figure 2 displays the "Number of Observations," "Dimensions," and the "Column Names of the Design Matrix" tables. The "Number of Observations" table shows that only 263 of the 322 observations in the input data are used, because some of the observations contain missing data. When you specify effects that contain classification variables, the number of parameters is usually larger than the number of effects. The "Dimensions" table shows the number of effects and the number of parameters that are considered.

The "Column Names of the Design Matrix" table lists all the parameters that are included in the design matrix and indicates the column name that is associated with each parameter. As presented in Figure 2, the model includes a total of six parameters (two categorical variables with two levels each, one interaction variable, and the intercept), and therefore the design matrix has six columns. Because the OUTPUT statement does not specify a PREFIX= option, the default prefix (col) is used for the column names. For more information about how to change the column names, see the PREFIX= option and Examples.

The "Model Information" table in Figure 3 shows the main components of the model.

Figure 3: Model Information Table

The MODELMATRIX Procedure

Model Information
Data SourceBASEBALL
Response VariablelogSalary


To be able to inspect the created design matrix, you first need to transfer the output data table (mylib.designMat) from the computing cluster to a local data table (designMat) and then use the PRINT procedure. Note that PROC MODELMATRIX does not preserve the order of the rows in the original data table. Therefore the design matrix that is produced is sorted before printing. The following statements show how you can use a DATA step along with a PROC PRINT step to display the first 15 rows of the transferred design matrix:


data designMat;
 set mylib.designMat;
run;

proc sort data=designMat;
 by index;
run;

proc print data = designMat(obs=15 drop=index) label;
label COL1='Intercept'
      COL2='League American'
      COL3='League National'
      COL4='Division East'
      COL5='Division West'
      COL6='nHits * YrMajor';
run;

Figure 4: First 15 Rows of the Design Matrix Stored in the designMat Data Table

ObsInterceptLeague
American
League
National
Division
East
Division
West
nHits * YrMajor
1......
2101011134
311001390
4101101551
510110174
6110011859
71011074
811001219
910101162
10110101196
11110101590
1210101477
1310110452
1411010360
1510110559


Last updated: June 22, 2026