FOREST Procedure

Getting Started: FOREST Procedure

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.

A common use of forest models is to predict whether a mortgage applicant will default on a loan. The home equity data table Hmeq, which is in the Sampsio library that SAS provides, contains observations for 5,960 mortgage applicants. A variable named Bad indicates whether the applicant, after being approved for a loan, paid off or defaulted on the loan.

This example uses the Hmeq data table to build a forest model that is used to score the data and can be used to score data about new loan applicants. Table 1 describes the variables in Hmeq.

Table 1: Variables in the Home Equity (Hmeq) Data Table

Variable Role Level Description
Bad Response Binary 1 = applicant defaulted on the loan or is seriously delinquent
0 = applicant paid off the loan
CLAge Predictor Interval Age of oldest credit line in months
CLNo Predictor Interval Number of credit lines
DebtInc Predictor Interval Debt-to-income ratio
Delinq Predictor Interval Number of delinquent credit lines
Derog Predictor Interval Number of major derogatory reports
Job Predictor Nominal Occupational category
Loan Predictor Interval Requested loan amount
MortDue Predictor Interval Amount due on mortgage
nInq Predictor Interval Number of recent credit inquiries
Reason Predictor Binary DebtCon = debt consolidation
HomeImp = home improvement
Value Predictor Interval Value of property
YoJ Predictor Interval Years at present job


The following statements load the mylib.hmeq data into your CAS session by specifying your CAS engine libref in the first statement. These statements assume that your CAS engine libref is named mylib, as in the section Using CAS Sessions and CAS Engine Librefs, but you can substitute any appropriately defined CAS engine libref.

/* Convert variable names to mixed case */
data mylib.hmeq;
   length Bad Loan MortDue Value 8 Reason Job $7
              YoJ Derog Delinq CLAge nInq CLNo DebtInc 8;
   set sampsio.hmeq;
run;

proc print data=mylib.hmeq(obs=10); run;

Figure 1 shows the first 10 observations of mylib.hmeq.

Figure 1: Partial Listing of the mylib.hmeq Data

ObsBadLoanMortDueValueReasonJobYoJDerogDelinqCLAgenInqCLNoDebtInc
1111002586039025HomeImpOther10.50094.36719.
211500..  .......
3118004864957037HomeImpOther5.03277.100117.
412000.62250HomeImpSales16.000115.800013.
5120004500055000HomeImpOther3.00086.067225.
6122002428034687HomeImpOther.01300.86708.
7123002819240150HomeImpOther4.50054.600116.
8124005000073395HomeImpProfExe5.010.10.
912400.17180HomeImpOther.0014.56734.
10125001500020200HomeImp 18.000136.067119.


PROC FOREST treats numeric variables as interval inputs unless you specify otherwise. Character variables are always treated as nominal inputs. The following statements run PROC FOREST and save the model in a table named mylib.savedModel:

proc forest data=mylib.hmeq outmodel=mylib.savedModel seed=12345;
   input Delinq Derog Job nInq Reason / level = nominal;
   input CLAge CLNo DebtInc Loan Mortdue Value YoJ / level = interval;
   target Bad / level = nominal;
   ods output FitStatistics=fitstats;
run;

No parameters are specified in the PROC FOREST statement; therefore, the procedure uses all default values. For example, the number of trees in the forest is 100, the number of bins for interval input variables is 20, and the number of variables that are examined at each node for a split is the square root of the number of input variables.

The INPUT and TARGET statements are required in order to run PROC FOREST. The INPUT statement indicates which variables to use to build the model, and the TARGET statement indicates which variable the procedure predicts.

Figure 2 displays the "Model Information" table. This table shows the values of the training parameters in the first six rows, in addition to some basic information about the trees in the forest.

Figure 2: Model Information

The FOREST Procedure

Model Information
Number of Trees100
Number of Variables Per Split4
Seed12345
Bootstrap Percentage60
Number of Bins50
Number of Input Variables12
Maximum Number of Tree Nodes329
Minimum Number of Tree Nodes143
Maximum Number of Branches2
Minimum Number of Branches2
Maximum Depth20
Minimum Depth20
Maximum Number of Leaves165
Minimum Number of Leaves72
Maximum Leaf Size2282
Minimum Leaf Size5
OOB Misclassification Rate0.09765101
Average Number of Leaves122.33


Figure 3 displays the "Number of Observations" table, which shows how many observations were read and used. If you specify a PARTITION statement, the "Number of Observations" table also displays the number of observations that were read and used per partition.

Figure 3: Number of Observations

 Training
Number of Observations Read5960
Number of Observations Used5960


Figure 4 displays the estimates of variable importance. The rows in this figure are sorted by the importance measure. A conclusion from fitting the forest model to these data is that DebtInc is the most important predictor of loan default.

Figure 4: Variable Importance

Variable Importance
VariableImportanceStd Dev
Importance
Relative
Importance
DebtInc384.1063.03761.0000
Value72.754925.48820.1894
Delinq58.606318.14540.1526
CLAge46.914412.09560.1221
Derog39.764915.54010.1035
CLNo31.27298.66600.0814
Loan30.974112.13800.0806
YoJ27.447610.74590.0715
Job26.82599.25560.0698
nInq26.38619.37980.0687
MortDue26.10409.83480.0680
Reason4.90374.31710.0128


Figure 5 shows the first 10 and last 10 observations of the fit statistics. When PROC FOREST runs, it computes fit statistics for a sequence of forests that have an increasing number of trees. As the number of trees increases, the fit statistics usually improve (decrease) at first and then level off and fluctuate within a small range. Forest models provide an alternative estimate of the average square error and misclassification rate. This alternative is called the out-of-bag (OOB) estimate. The OOB estimate is a convenient substitute for an estimate that is based on test data and is a less biased estimate of how the model will perform on future data. For more information, see the section Bagging the Data. The listing shows that the out-of-bag error estimate is worse (larger) than the estimate that evaluates all observations on all trees. This is common.

Figure 5: Fit Statistics

Fit Statistics
Number
of Trees
OOB
Average
Square Error
Training
Average
Square Error
OOB
Misclassification
Rate
Training
Misclassification
Rate
OOB
Log
Loss
Training
Log Loss
10.10590.07880.13470.10371.3010.777
20.09790.06690.12660.08710.9150.290
30.09420.06270.12340.08020.7070.226
40.09090.06150.11970.08170.5860.212
50.08670.05950.11750.07600.4520.207
60.08400.05830.11330.07680.4030.203
70.08250.05720.11200.07430.3540.200
80.08080.05720.10980.07300.3260.200
90.08010.05690.10810.07320.3070.199
100.07930.05680.10790.07500.2970.198
.......
.......
.......
910.07360.05500.09800.07420.2490.193
920.07360.05490.09820.07400.2490.193
930.07360.05490.09870.07400.2490.193
940.07360.05500.09830.07380.2490.193
950.07370.05500.09780.07470.2490.193
960.07370.05500.09750.07450.2490.193
970.07370.05500.09780.07430.2490.194
980.07370.05500.09680.07380.2490.194
990.07370.05500.09710.07370.2490.193
1000.07370.05500.09770.07370.2490.193


Last updated: October 13, 2026