Regression Action Set
The modelMatrix Action
Producing a Design Matrix for Regression Models
This section contains PROC CAS code.
Note: Input data must be accessible in your CAS session, either as a CAS table or as a transient-scope table. A CAS table has a two-level name: the first level is your CAS engine libref, and the second level is the table name. You refer to this table in the CAS procedure by specifying only the second level. For more information about two-level names, see Chapter 2, Shared Concepts. A transient-scope table is called directly from the action and exists in memory for the duration of the action. For more information about accessing data, see SAS Viya: System Programming Guide. For more information about PROC CAS and programming in CASL, see SAS Cloud Analytic Services: CASL Programmer’s Guide and SAS Cloud Analytic Services: CASL Reference.
This example demonstrates several features of the modelMatrix action for creating a design matrix for regression models. It shows the use of classification variables in a model parameter along with the use of interaction effects, spline effects, and BY-group processing in the modelMatrix action. In some cases, you can use the ODS system in conjunction with PROC CAS as an alternative to the techniques shown in this example. This example uses features of the action instead of using ODS because these features are available on Lua, Python, and Java clients, where ODS is not available.
Efficient Data Simulation on the CAS Server
The following DATA step runs on the CAS server and creates the data table mycas.sample, which contains 20 observations and eight variables. This DATA step assumes that your CAS engine libref is named mycas and your CAS session is named mysess, but you can substitute any appropriately defined CAS engine libref and session name.
The DATA step forms the response y by using a random number generator. The variable x is used as a continuous regressor, and the variable c is used as a classification variable.
data mycas.sample / sessref=mysess single=yes;
call streamInit(1);
do byVar=1 to 2;
do i=1 to 10;
if byVar=1 then c=rand("Integer",0,3);
else if byVar=2 then c=rand("Integer",0,1);
x=rand("Normal");
y=1;
wgt=1;
if i=5 and byVar=2 then frq = -1; else frq=1;
index + 1;
output;
end;
end;
run;
The preceding DATA step generates a data table (sample) that has 20 observations. It contains two continuous variables (x and y) and a single classification variable (c), which can have a maximum of four unique values for 10 observations and a maximum of two unique values for the rest of the observations. Also, a BY variable named byVar is defined that divides the data table into two equally sized BY groups, each of which has 10 observations. Also included are the frq and wgt variables that are used as frequency and weight variables, respectively. For simplicity, all the weight variable values are assigned to 1. The same applies to the frequency variable, with the exception of the observation whose index value is 15, which has an invalid frequency variable value (that is, –1). This invalid frequency variable value demonstrates how the modelMatrix action treats observations that have invalid frequency variable values.
In the following sections, a simple model definition is presented first, followed by more complex statements to highlight the key features of the modelMatrix action. The preceding simulated data table (sample) is used in the use cases that are presented.
Model Definition
The following code shows how you can specify a model definition by using the model parameter in the modelMatrix action:
regression.modelMatrix /
table='sample',
class={'/c/'},
model={ depVar='y',
effects={'/c/','x'}
},
outDesign={casOut='designMat',
prefix='param'
};
run;
The specified model is a linear combination of the main effects, x; all levels of c; and the interaction effect, x*c.
The outDesign parameter is required in the modelMatrix action. The following three subparameters are defined for the outDesign parameter:
casOut: requiredprefix: optional; the default value isColcopyVars: optional
The casOut subparameter defines the name of the design matrix and is required. The prefix subparameter accepts a valid SAS prefix (and replaces the default prefix Col) and is optional. The specified prefix, appended to a one-based index, forms the column names of the design matrix. The copyVars subparameter appends some or all of the variables of the input data table to the design matrix. By default, no input variable is appended to the design matrix.
You can use Perl regular expressions to specify sets of variables in all the variable lists that you can specify in the class and model parameters.
Alternatively, the modelMatrix action supports the machine-learning syntax mode for model specification. For examples of this syntax, see Model Selection with Validation.
Saving the Action Output in a Named Result Object
In the previous code example, the output of the modelMatrix action is immediately displayed. If you assign the results to a named result object, then the output is saved in that result object and is not displayed. The following code shows how you can perform this task:
regression.modelMatrix result=myresult /
table='sample',
class={'/c/'},
model={ depVar='y',
effects={'/c/','x'}
},
outDesign={casOut={name='designMat',
replace='True'
},
prefix='param'
};
run;
After you run the preceding code, the results of running the modelMatrix action are stored in the result object named myresult, but no output is displayed. You can display the tables that are stored in the myresult object by using the PRINT statement in PROC CAS as follows:
print myresult;
run;
The output of running this PRINT statement is the same as when you run the modelMatrix action without assigning the results; it is not shown here. You can also display specific tables that are stored in the myresult object. The following code shows how you can print the "Number of Observations" table:
print myresult['NObs'];
run;
The output is shown in Output 20.14.1.
Output 20.14.1: Tables Produced from myresult
| Number of Observations Read | 20 |
|---|---|
| Number of Observations Used | 20 |
In order to use the PRINT statement to print specific tables in a stored result object, you need to know the paths of those tables in the stored result object. One way you can get this information is to specify the ODS TRACE ON command and then print the entire result. Another way to get the paths is to use the DESCRIBE statement in PROC CAS as follows:
describe myresult;
run;
This displays a dictionary of the tables in the SAS log and the columns in those tables that are stored in the myresult object. A subset of this dictionary for the "ModelInfo" table follows:
dictionary ( 5 entries, 5 used); [ModelInfo] Table ( [2] Rows [3] columns Column Names: [1] RowId [Name ] (varchar) [2] Description [Description ] (varchar) [3] Value [Value ] (varchar)
This dictionary contains the paths (such as NObs) of all tables in the myresult object. You must use the complete path to request specific tables in the PRINT statement.
Using the display Parameter
The modelMatrix action supports a display parameter, which provides functionality that corresponds to that of the ODS SELECT, ODS EXCLUDE, and ODS TRACE statements on a SAS client. The major advantage of using the action’s display parameter is that you can use it with Lua, Python, and Java clients, where the ODS system is not available. The following code shows how you can use the traceNames subparameter of the display parameter to obtain the paths of the tables that the modelMatrix action produces:
regression.modelMatrix result=myresult /
table='sample',
class={'/c/'},
model={ depVar='y',
effects={'/c/','x'}
},
outDesign={casOut={name='designMat',
replace='True'
},
prefix='param'
},
display={traceNames=true};
run;
This code produces the following notes in the SAS log:
NOTE: Name:'OutDesignInfo' Label:'Design Matrix Column Information'
Path:'OutDesignInfo'
NOTE: Name:'ModelInfo' Label:'Model Information' Path:'ModelInfo'
NOTE: Name:'NObs' Label:'Number of Observations' Path:'NObs'
NOTE: Name:'Dimensions' Label:'Dimensions' Path:'Dimensions'
NOTE: Name:'ClassInfo' Label:'Class Level Information' Path:'ClassInfo'
You can also use the display parameter to select which tables the modelMatrix action produces are returned to the client. The following code uses the display parameter to select the "OutDesignInfo" and "NObs" tables:
regression.modelMatrix /
table='sample',
class={'/c/'},
model={ depVar='y',
effects={'/c/','x'}
},
outDesign={casOut={name='designMat',
replace='True'
},
prefix='param'
},
display={names={'OutDesignInfo','NObs'}};
run;
Specifying full paths is optional—the path-matching rules that the display parameter uses are consistent with the rules that the ODS SELECT statement uses. The tables that you request by using the display parameter match all tables whose pathnames end with the specified path. For example, display={names={"NObs"}} matches all the paths NObs, BYGroup1.SelectedModel.NObs, and so on.
Figure 1 shows the filtered output that contains only the "Design Matrix Column Information" and "Number of Observations" tables.
Figure 1: Filtered Output from the modelMatrix Action
| Number of Observations Read | 20 |
|---|---|
| Number of Observations Used | 20 |
| Column Names of the Design Matrix | |
|---|---|
| Name | Parameter |
| param1 | Intercept |
| param2 | c 0 |
| param3 | c 1 |
| param4 | c 2 |
| param5 | c 3 |
| param6 | x |
If you enclose the strings in the names subparameter of the display parameter in forward slashes, then those strings are treated as Perl regular expressions that are matched against the full paths of the tables that the action produces. For example, display={names={’/NObs/’}} selects the paths BYGroup1.NObs and BYGroup2.NObs.
Saving Displayed Tables as CAS Tables by Using the outputTables Parameter
The modelMatrix action supports an outputTables parameter, which enables you to save any displayed table as a CAS table in the active caslib. This functionality is analogous to that of the ODS OUTPUT statement, which saves ODS output as SAS data sets. The following code provides an example of using this parameter:
regression.modelMatrix /
table='sample',
class={'/c/'},
model={ depVar='y',
effects={'/c/','x'}
},
outDesign={casOut={name='designMat',
replace='True'
},
prefix='param'
},
outputTables={names={'OutDesignInfo', 'NObs'}};
run;
Output 20.14.2 displays the output CAS tables, which provide information about all the CAS tables that the action produces. You see that the preceding code creates CAS tables named "OutDesignInfo" and "NObs."
Output 20.14.2: Output CAS Tables
| Output CAS Tables | |||
|---|---|---|---|
| CAS Library | Name | Number of Rows | Number of Columns |
| CASUSERHDFS(sasred) | designMat | 20 | 6 |
| CASUSERHDFS(sasred) | OutDesignInfo | 6 | 3 |
| CASUSERHDFS(sasred) | NObs | 2 | 3 |
The following PROC PRINT code displays the "NObs" CAS table on the SAS client by using the CAS engine to bring the table back to the SAS client:
proc print data=mycas.NObs;
run;
Output 20.14.3 shows contents of the "NObs" CAS table.
Output 20.14.3: "NObs" CAS Table
| Obs | RowId | Description | Value |
|---|---|---|---|
| 1 | NREAD | Number of Observations Read | 20 |
| 2 | NUSED | Number of Observations Used | 20 |
The CAS tables that you request in the outputTables parameter consist of all the tables whose paths end with the specified name. Grouping variables are added to the CAS table so that you can distinguish between the tables.
Storing the Design Matrix
You can use the outDesign parameter to create an output CAS table that contains the design matrix of the defined model. The outDesign parameter is required in the modelMatrix action. The variables in the input data table are not included in the output data table in order to avoid data duplication for large data tables; however, variables that you specify in the copyVars subparameter are included. If you specify strings enclosed in forward slashes in the copyVars subparameter, they are interpreted as Perl regular expressions.
The following code demonstrates several features of the modelMatrix action. It shows the use of interaction and spline effects in a model parameter. It also shows how the action performs BY-group processing for a particular model. The same data table (mycas.sample) that is defined at the beginning of this section is used here.
regression.modelMatrix /
table={name='sample', groupBy='byVar'},
nThreads=1,
class = {'c'},
spline = {{name='spl',vars={'x'}}},
model ={
depVar={'y'},
effects={'x','c','spl',
{vars={'x','c'}, interact='CROSS'}
}
},
freq = 'frq',
weight = 'wgt',
outDesign={casOut={name='designMat',
replace='True'
},
prefix ='param',
copyVars={'c', 'frq', 'index'}
};
run;
The preceding statements create a model that contains the continuous variable x and the classification variable c. The model includes the variable x, individual levels of the variable c, a spline effect on the variable x, and the cross interaction of the variables x and c (x*c). BY-group processing is performed on the basis of the variable byVar in the data table.
The preceding lines of code use the following additional parameters:
groupby: The codegroupBy=’byVar’declares the BY-group processing variable.nThreads: ThenThreadsparameter sets the number of threads to use on each worker node.spline: ThemodelMatrixaction accepts polynomial and spline effects in amodelparameter.cross: ThemodelMatrixaction supports cross and multilevel interactions as part of theeffectsparameter.freqandweight: ThemodelMatrixaction groups and weights observations by using thefreqandweightparameters, respectively.
Several output tables are displayed consecutively. They are shown in Output 20.14.4 through Output 20.14.6.
Output 20.14.4: Model Information and Column Names of the Design Matrix Tables
| Model Information | |
|---|---|
| Data Source | SAMPLE |
| Response Variable | y |
| Frequency Variable | frq |
| Weight Variable | wgt |
| Column Names of the Design Matrix | |
|---|---|
| Name | Parameter |
| param1 | Intercept |
| param2 | x |
| param3 | c 1 |
| param4 | c 2 |
| param5 | c 3 |
| param6 | c 0 |
| param7 | spl 1 |
| param8 | spl 2 |
| param9 | spl 3 |
| param10 | spl 4 |
| param11 | spl 5 |
| param12 | spl 6 |
| param13 | spl 7 |
| param14 | x * c 1 |
| param15 | x * c 2 |
| param16 | x * c 3 |
| param17 | x * c 0 |
The "Column Names of the Design Matrix" table in Output 20.14.4 shows that the defined model creates a total of 17 parameters across the two BY groups. Therefore, the design matrix has 17+3 columns (the additional columns are reserved for the variables c, frq, and index that are appended to the design matrix by using the copyVars keyword).
Apart from the intercept, x as a continuous variable is considered directly in the model. However, because c is defined as a classification variable, all of its unique values (0–3) are considered as separate parameters (a total of four). You can see that the included spline effect adds seven more parameters to the model. Finally, the interaction of x and c () adds four more parameters to the model because of the interaction of the continuous
x with all four levels of c.
Also displayed in Output 20.14.4 is the "Model Information" table, which shows a brief summary of the response variable and the frequency and weight variables.
The rest of the output tables are dependent on the BY group and hence are produced separately. They are shown in Output 20.14.5 and Output 20.14.6.
Output 20.14.5: Number of Observations, Class Level Information, and Dimensions Tables for BY Group 1
| Number of Observations Read | 10 |
|---|---|
| Number of Observations Used | 10 |
| Sum of Frequencies Read | 10 |
| Sum of Frequencies Used | 10 |
| Sum of Weights Read | 10 |
| Sum of Weights Used | 10 |
| Class Level Information | ||
|---|---|---|
| Class | Levels | Values |
| c | 3 | 1 2 3 |
| Dimensions | |
|---|---|
| Number of Effects | 5 |
| Number of Parameters | 15 |
Output 20.14.6: Number of Observations, Class Level Information, and Dimensions Tables for BY Group 2
| Number of Observations Read | 10 |
|---|---|
| Number of Observations Used | 9 |
| Sum of Frequencies Read | 9 |
| Sum of Frequencies Used | 9 |
| Sum of Weights Read | 9 |
| Sum of Weights Used | 9 |
| Class Level Information | ||
|---|---|---|
| Class | Levels | Values |
| c | 2 | 0 1 |
| Dimensions | |
|---|---|
| Number of Effects | 5 |
| Number of Parameters | 13 |
For each BY group, the modelMatrix action displays the number of observations that are read and used. Because the frequency and weight variables are defined, the action displays the number of frequency variables that are read and used and the sum of the weight variables. You can see that BY group 2 (Output 20.14.5) has one invalid observation (the observation whose index value is 15). Therefore, the number of used frequency variables is nine; this is different from BY group 1, in which all frequency variables have a value of 1.
The "Dimensions" table shows the number of effects and parameters that are considered within each BY group. Notice that the number of considered effects for both BY groups is five. However, neither BY group includes all the parameters listed in the OutDesignInfo table; so, for all BY groups, the number of parameters is less than 17.
This fact can be seen in the "Class Level Information" tables shown in Output 20.14.5 and Output 20.14.6. These tables show the levelization of the classification variables within each BY group (in this case only c is defined as a classification variable). You can see that none of the BY groups include all levels of the c (that is, 0, 1, 2 and 3) which is why the number of parameters is less than 17 for all BY groups.
The modelMatrix action constructs a global design matrix. Therefore, it constructs a table that includes the union of all parameters in each BY group. To inspect the design matrix that resides on the CAS server as a CAS table, you can transfer the constructed table with 20 rows to the client by using the following statements:
data designMat;
set mycas.designMat;
run;
proc print data=designMat;
format BEST 5. param1-param17;
var index c frq param1-param7 param17;
where
(index < 11) or
(index = 15);
run;
Output 20.14.7: Rows with index Values of 1–10 and Row with index Value of 15 from Design Matrix
| Obs | index | c | frq | param1 | param2 | param3 | param4 | param5 | param6 | param7 | param17 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 1 | 3 | 1 | 1 | 0.46319 | 0 | 0 | 1 | . | 0.00000 | . |
| 2 | 2 | 3 | 1 | 1 | 0.00741 | 0 | 0 | 1 | . | 0.00000 | . |
| 3 | 3 | 2 | 1 | 1 | -0.06703 | 0 | 1 | 0 | . | 0.00000 | . |
| 4 | 4 | 3 | 1 | 1 | 1.52070 | 0 | 0 | 1 | . | 0.00000 | . |
| 5 | 5 | 2 | 1 | 1 | 0.51495 | 0 | 1 | 0 | . | 0.00000 | . |
| 6 | 6 | 3 | 1 | 1 | 1.00574 | 0 | 0 | 1 | . | 0.00000 | . |
| 7 | 7 | 3 | 1 | 1 | 0.36049 | 0 | 0 | 1 | . | 0.00000 | . |
| 8 | 8 | 3 | 1 | 1 | -1.75802 | 0 | 0 | 1 | . | 0.16667 | . |
| 9 | 9 | 2 | 1 | 1 | 1.02322 | 0 | 1 | 0 | . | 0.00000 | . |
| 10 | 10 | 1 | 1 | 1 | 0.33004 | 1 | 0 | 0 | . | 0.00000 | . |
| 15 | 15 | 0 | -1 | . | . | . | . | . | . | . | . |
Output 20.14.7 displays rows of the design matrix whose index value is 1–10 and the row whose index is 15. Only the index, c, frq, param1–param7, and param17 variables are shown. As expected, the generated data table contains 20 rows associated with each observation and 20 columns (17 parameters plus three columns for input variable c, frq, and index). You can see that the column names are param1 through param17, based on the specified prefix subparameter in the outDesign parameter.
The OutDesignInfo table lists the model parameters along with their corresponding column names in the design matrix. Columns param3 through param6 contain all levels of the c variable. The rows in the design matrix whose index value is less than 11 belong to the first BY group (byVar value of 1), and according to the ClassInfo table, level does not exist in either BY group. This observation can be confirmed by the fact that the
param6 and param17 columns of the design matrix (which are associated with the and
parameters, respectively) are empty.
You can also see that the row of the design matrix whose index value is 15 is all blanks. This is because the defined frequency variable value for this observation is invalid and equal to –1 (see the frq column in the design matrix), and therefore this observation is not considered when the design matrix is created.
Producing a Design Matrix for Regression Models
This section contains Lua code that creates the same sample table that is shown in the CASL example. It highlights sections of the example where the Lua syntax differs significantly from the CASL syntax. See the CASL version of this example for discussions of the data, syntax features, and the output from running these examples. For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.
Because this example uses simulated data that are produced from DATA step code, you can submit the runCode action on your Lua client as follows to create the simulated data in your CAS session:
s:runCode{single='YES',code="\
data sample;\
call streamInit(1);\
do byVar=1 to 2;\
do i=1 to 10;\
if byVar=1 then c=rand('Integer',0,3);\
else if byVar=2 then c=rand('Integer',0,1);\
x=rand('NORMAL');\
y=1;\
wgt = 1;\
if i=5 and byVar=2 then frq = -1; else frq=1;\
index + 1;\
output;\
end;\
end;\
run;"
}
Loading the regression Action Set and Running the modelMatrix Action
The following code loads the regression action set:
s:loadactionset{actionset='regression'}
You can specify an action by using the action set name and the action name, separated by an underscore (actionset_action). For example, you can specify the name regression_modelMatrix to run the modelMatrix action in the regression action set, as shown in the following code:
s:regression_modelMatrix{
table='sample',
class={'c'},
model={ depVar='y',
effects={'c','x'}
},
outDesign={casOut='designMat',
prefix='param'
}
}
The following code shows that you do not have to specify the action set name when you run the modelMatrix action:
s:modelMatrix{
table='sample',
class={'c'},
model={ depVar='y',
effects={'c','x'}
},
outDesign={casOut={name='designMat',
replace=true
},
prefix='param'
}
}
Saving the Results of an Action
As in the CASL case, you can submit actions from Lua without specifying a named result, as shown in the following code:
s:modelMatrix{
table='sample',
class={'/c/'},
model={ depVar='y',
effects={'/c/','x'}
},
outDesign={casOut={name='designMat',
replace=true
},
prefix='param'
}
}
When you submit the preceding code, the results are sent to the Lua client and are displayed immediately as the action runs. If you want to save the results, you can name the results as shown in the following code:
myResult=s:modelMatrix{
table='sample',
class={'/c/'},
model={ depVar='y',
effects={'/c/','x'}
},
outDesign={casOut={name='designMat',
replace=true
},
prefix='param'
}
}
In this case, the results are not displayed but are stored in a variable named myResult. You can display the contents of the myResult variable by entering myResult at the Lua prompt. This displays all the tables that are produced by this action call along with their full pathnames. You can specify table pathnames in a print command to display specific tables, as shown in the following code:
print(myResult.OutDesignInfo)
print(myResult.ModelInfo)
print(myResult.NObs)
print(myResult.Dimensions)
print(myResult.ClassInfo)
Saving the Status of an Action
The following code shows how you can save the status of an action call by providing a name for the status variable after you specify a name for the results:
myResult,myStatus=s:modelMatrix{
table='sample',
class={'/c/'},
model={ depVar='y',
effects={'/c/','x'}
},
outDesign={casOut={name='designMat',
replace=true
},
prefix='param'
}
}
The preceding code saves the status of the action submission in the variable myStatus. You can display the contents of the myStatus variable by entering myStatus at the Lua prompt.
For more information about severity and reason codes, see the section "Programming with Severity and Reason Codes" in SAS Viya: System Programming Guide.
A Full-Featured Example
The following code shows many features of the modelMatrix action, including the following:
processing groups by using the
groupBysubparameter of thetableparameterassigning groups and weights to the observations by using the
freqandweightparametersusing Perl regular expressions to define variable lists
subsetting results by using the
displayparametersaving specified results as CAS tables by using the
outputTablesparametercreating an output CAS table to contain the design matrix by using the
outDesignparameter
myResult,myStatus=s:modelMatrix{
table={name = 'sample', groupBy='byVar'},
class={'c'},
spline={{name= 'spl', vars='x'}},
model={ depVar='y',
effects={ 'x', '/c/', 'spl',
{ vars={'x','c'}, interact='CROSS' }
}
},
display={names={'/NObs/'}},
outputTables={names={NObs='myNObs',
Dimensions='myDimensions'
}
},
freq='frq',
weight='wgt',
outDesign={casOut={name='designMat',
replace=true
},
prefix='param'
}
}
The outputTables parameter saves the "Number of Observations" and "Dimensions" tables as CAS tables named myNObs and myDimensions, respectively. The outDesign (or output) parameter saves the design matrix in a CAS table named designMat. You can obtain the details about the myOutputTable table by running the tableInfo action as shown in the following code:
s:tableInfo{table='designMat'}
For explanations and output of other features highlighted in this section, see the CASL example.
Producing a Design Matrix for Regression Models
This section contains Python code that creates the same sample table that is shown in the CASL example. It highlights sections of the example where the Python syntax differs significantly from the CASL syntax. See the CASL version of this example for discussions of the data, syntax features, and the output from running these examples. For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.
Because this example uses simulated data that are produced from DATA step code, you can submit the runCode action on your Python client as follows to create the simulated data in your CAS session:
s.runCode(single='YES',code="\
data sample;\
call streamInit(1);\
do byVar=1 to 2;\
do i=1 to 10;\
if byVar=1 then c=rand('Integer',0,3);\
else if byVar=2 then c=rand('Integer',0,1);\
x=rand('NORMAL');\
y=1;\
wgt = 1;\
if i=5 and byVar=2 then frq = -1; else frq=1;\
index + 1;\
output;\
end;\
end;\
run;"
)
Loading the regression Action Set and Running the modelMatrix Action
The following code loads the regression action set:
s.loadactionset(actionset='regression')
You can specify an action by using the action set name and the action name, separated by a dot (actionset.action). For example, you can use the name regression.modelMatrix to run the modelMatrix action in the regression action set, as shown in the following code:
s.regression.modelMatrix(
table='sample',
classVars=['c'],
model={ 'depVar':'y',
'effects':['c','x']
},
outDesign={'casOut':'designMat',
'prefix':'param'
}
)
The following code shows that you do not have to specify the action set name when you run the modelMatrix action:
s.modelMatrix(
table='sample',
classVars=['c'],
model={ 'depVar':'y',
'effects':['c','x']
},
outDesign={'casOut':{'name':'designMat',
'replace':True
},
'prefix':'param'
}
)
Saving the Results of an Action
As in the CASL case, you can submit actions from Python without specifying a named result, as shown in the following code:
s.modelMatrix(
table='sample',
classVars='/c/',
model={ 'depVar':'y',
'effects':['/c/','x']
},
outDesign={'casOut':{'name':'designMat',
'replace':True
},
'prefix':'param'
}
)
When you submit the preceding code, the results are sent to the Python client and are displayed immediately as the action runs. If you want to save the results, you can name the results as shown in the following code:
myResult = s.modelMatrix(
table='sample',
classVars='/c/',
model={ 'depVar':'y',
'effects':['/c/','x']
},
outDesign={'casOut':{'name':'designMat',
'replace':True
},
'prefix':'param'
}
)
In this case, the results are not displayed but are stored in a variable named myResult. The following command displays all the tables that are produced by this action call along with their full pathnames:
print(myResult)
The following commands individually display some of the tables that this action call produces:
print(myResult.OutDesignInfo)
print(myResult.ModelInfo)
print(myResult.NObs)
print(myResult.Dimensions)
print(myResult.ClassInfo)
Status information about the action is also saved in myResult. You can display the severity, reason, and status by using the print command as follows:
print("Severity code:", myResult.severity)
print("Reason: ", myResult.reason)
print("Status: ", myResult.status)
For more information about severity and reason codes, see the section "Programming with Severity and Reason Codes" in SAS Viya: System Programming Guide.
A Full-Featured Example
The following code shows many features of the modelMatrix action, including the following:
processing groups by using the
groupBysubparameter of thetableparameterusing of crossed and spline effects in the
effectssubparameter of themodelparameterassigning groups and weights to the observations by using the
freqandweightparametersusing Perl regular expressions to define variable lists
subsetting results by using the
displayparametersaving specified results as CAS tables by using the
outputTablesparametercreating an output CAS table to contain the design matrix by using the
outDesignparameter
myResult=s.modelMatrix(
table={'name':'sample','groupBy':'byVar'},
classVars=['c'],
spline={'name':{'name':'spl','vars':'x'}},
model={ 'depVar':'y',
'effects':['x', '/c/','spl',
{ 'vars':{'x','c'},'interact':'CROSS'}]
},
display={'names':{'/NObs/'}},
outputTables={'names':{'NObs':'myNObs',
'Dimensions':'myDimensions'}
},
freq='frq',
weight='wgt',
outDesign={'casOut':{'name':'designMat',
'replace':True
},
'prefix':'param'
}
)
The outputTables parameter saves the "Number of Observations" and "Dimensions" tables as CAS tables named myNObs and myDimensions, respectively. The outDesign (or output) parameter saves the design matrix in a CAS table named designMat. You can obtain the details about the myOutputTable table by running the tableInfo action as shown in the following code:
s.tableInfo(table='designMat')
For explanations and output of other features highlighted in this section, see the CASL example.
Producing a Design Matrix for Regression Models
This example is not available for the R programming language.