The examples use the Sashelp.JunkMail dataset, which is loaded into a temporary CAS table named mycas.junkmail for procedure execution.
1 Code Block
PROC FOREST Data
Explanation : This example initializes a CAS session and loads the 'JunkMail' dataset from Sashelp into a CAS table named 'mycas.junkmail'. It then trains a basic random forest model using the FOREST procedure with default parameters, except for a seed for reproducibility. The model's fit statistics are saved and displayed.
Copied!
caslib _all_ assign;
/* Charger le jeu de données Sashelp.JunkMail dans une table CAS temporaire */
data mycas.junkmail;
set sashelp.junkmail;
run;
/* Entraîner un modèle Forest de base */
proc forest data=mycas.junkmail seed=54321;
input Address Addresses All Bracket Business CS CapAvg CapLong
CapTotal Conference Credit Data Direct Dollar Edu Email
Exclamation Font Free George HP HPL Internet Lab Labs
Mail Make Meeting Money Order Original Our Over PM Paren
Parts People Pound Project RE Receive Remove Semicolon
Table Technology Telnet Will You Your _000 _85 _415 _650
_857 _1999 _3D / level = interval;
target class /level=nominal;
ods output FitStatistics=basic_fit_stats;
run;
/* Afficher les statistiques d'ajustement */
proc print data=basic_fit_stats;
run;
1
caslib _all_ assign;
2
3
/* Charger le jeu de données Sashelp.JunkMail dans une table CAS temporaire */
4
DATA mycas.junkmail;
5
SET sashelp.junkmail;
6
RUN;
7
8
/* Entraîner un modèle Forest de base */
9
PROC FORESTDATA=mycas.junkmail seed=54321;
10
INPUT Address Addresses All Bracket Business CS CapAvg CapLong
11
CapTotal Conference Credit DATA Direct Dollar Edu Email
12
Exclamation Font Free George HP HPL Internet Lab Labs
13
Mail Make Meeting Money Order Original Our Over PM Paren
14
Parts People Pound Project RE Receive Remove Semicolon
15
TABLE Technology Telnet Will You Your _000 _85 _415 _650
16
_857 _1999 _3D / level = interval;
17
target class /level=nominal;
18
ods OUTPUT FitStatistics=basic_fit_stats;
19
RUN;
20
21
/* Afficher les statistiques d'ajustement */
22
PROC PRINTDATA=basic_fit_stats;
23
RUN;
2 Code Block
PROC FOREST
Explanation : This example extends the basic use case by incorporating tree growth options (NODESIZE and MAXDEPTH) to control tree complexity. It also uses cross-validation (CROSSVALIDATION CV=5) to more robustly evaluate model performance. Predicted scores are saved, and the first observations are displayed.
Copied!
caslib _all_ assign;
/* Charger le jeu de données Sashelp.JunkMail dans une table CAS temporaire */
data mycas.junkmail;
set sashelp.junkmail;
run;
/* Entraîner un modèle Forest avec validation croisée et options d'arbre */
proc forest data=mycas.junkmail seed=67890;
input Address Addresses All Bracket Business CS CapAvg CapLong
CapTotal Conference Credit Data Direct Dollar Edu Email
Exclamation Font Free George HP HPL Internet Lab Labs
Mail Make Meeting Money Order Original Our Over PM Paren
Parts People Pound Project RE Receive Remove Semicolon
Table Technology Telnet Will You Your _000 _85 _415 _650
_857 _1999 _3D / level = interval;
target class /level=nominal;
grow nodesize=5 maxdepth=10; /* Options courantes pour la croissance des arbres */
crossvalidation cv=5; /* Validation croisée à 5 plis */
output out=mycas.forest_cv_scores predicted_class;
ods output FitStatistics=cv_fit_stats;
run;
/* Afficher les premières observations des scores de validation croisée */
proc print data=mycas.forest_cv_scores (obs=10);
run;
1
caslib _all_ assign;
2
3
/* Charger le jeu de données Sashelp.JunkMail dans une table CAS temporaire */
4
DATA mycas.junkmail;
5
SET sashelp.junkmail;
6
RUN;
7
8
/* Entraîner un modèle Forest avec validation croisée et options d'arbre */
9
PROC FORESTDATA=mycas.junkmail seed=67890;
10
INPUT Address Addresses All Bracket Business CS CapAvg CapLong
11
CapTotal Conference Credit DATA Direct Dollar Edu Email
12
Exclamation Font Free George HP HPL Internet Lab Labs
13
Mail Make Meeting Money Order Original Our Over PM Paren
14
Parts People Pound Project RE Receive Remove Semicolon
15
TABLE Technology Telnet Will You Your _000 _85 _415 _650
16
_857 _1999 _3D / level = interval;
17
target class /level=nominal;
18
grow nodesize=5 maxdepth=10; /* Options courantes pour la croissance des arbres */
19
crossvalidation cv=5; /* Validation croisée à 5 plis */
/* Afficher les premières observations des scores de validation croisée */
25
PROC PRINTDATA=mycas.forest_cv_scores (obs=10);
26
RUN;
3 Code Block
PROC FOREST
Explanation : This advanced example uses the FOREST procedure to train a model while performing variable selection based on their importance (SELECTION METHOD=VARIABLEIMPORTANCE). The IMPORTANCE statement is added to display the contribution of each variable to the model. Predicted scores are saved, and variable importance is displayed in a separate table.
Copied!
caslib _all_ assign;
/* Charger le jeu de données Sashelp.JunkMail dans une table CAS temporaire */
data mycas.junkmail;
set sashelp.junkmail;
run;
/* Entraîner un modèle Forest avec sélection de variables et importance */
proc forest data=mycas.junkmail seed=98765;
input Address Addresses All Bracket Business CS CapAvg CapLong
CapTotal Conference Credit Data Direct Dollar Edu Email
Exclamation Font Free George HP HPL Internet Lab Labs
Mail Make Meeting Money Order Original Our Over PM Paren
Parts People Pound Project RE Receive Remove Semicolon
Table Technology Telnet Will You Your _000 _85 _415 _650
_857 _1999 _3D / level = interval;
target class /level=nominal;
selection method=variableimportance; /* Sélection de variables basée sur l'importance */
importance; /* Demander l'affichage de l'importance des variables */
output out=mycas.forest_importance_scores predicted_class;
ods output FitStatistics=importance_fit_stats VariableImportance=var_importance_table;
run;
/* Afficher l'importance des variables */
proc print data=var_importance_table;
run;
/* Afficher les premières observations des scores */
proc print data=mycas.forest_importance_scores (obs=10);
run;
1
caslib _all_ assign;
2
3
/* Charger le jeu de données Sashelp.JunkMail dans une table CAS temporaire */
4
DATA mycas.junkmail;
5
SET sashelp.junkmail;
6
RUN;
7
8
/* Entraîner un modèle Forest avec sélection de variables et importance */
9
PROC FORESTDATA=mycas.junkmail seed=98765;
10
INPUT Address Addresses All Bracket Business CS CapAvg CapLong
11
CapTotal Conference Credit DATA Direct Dollar Edu Email
12
Exclamation Font Free George HP HPL Internet Lab Labs
13
Mail Make Meeting Money Order Original Our Over PM Paren
14
Parts People Pound Project RE Receive Remove Semicolon
15
TABLE Technology Telnet Will You Your _000 _85 _415 _650
16
_857 _1999 _3D / level = interval;
17
target class /level=nominal;
18
selection method=variableimportance; /* Sélection de variables basée sur l'importance */
19
importance; /* Demander l'affichage de l'importance des variables */
Explanation : This example, as presented in the documentation, demonstrates the complete process of saving and reusing a model. First, a model is trained on 'mycas.junkmail' and saved using the OUTMODEL= option into 'mycas.forest_model'. Then, this saved model is loaded via the INMODEL= option to score 'new' data (in this example, the same data is used for demonstration purposes). Predicted scores are saved in 'mycas.score_later', and the first observations are displayed, illustrating the ability to apply a pre-trained model.
Copied!
caslib _all_ assign;
/* Charger le jeu de données Sashelp.JunkMail dans une table CAS temporaire */
data mycas.junkmail;
set sashelp.junkmail;
run;
/* Entraîner un modèle Forest et le sauvegarder (première exécution) */
proc forest data=mycas.junkmail outmodel=mycas.forest_model seed=12345;
input Address Addresses All Bracket Business CS CapAvg CapLong
CapTotal Conference Credit Data Direct Dollar Edu Email
Exclamation Font Free George HP HPL Internet Lab Labs
Mail Make Meeting Money Order Original Our Over PM Paren
Parts People Pound Project RE Receive Remove Semicolon
Table Technology Telnet Will You Your _000 _85 _415 _650
_857 _1999 _3D / level = interval;
target class /level=nominal;
output out=mycas.score_at_runtime predicted_class;
run;
/* Utiliser le modèle sauvegardé pour scorer de nouvelles données (ici, les mêmes données pour démonstration) */
/* (Simule le scoring de 'nouvelles' données à un moment ultérieur ou sur un autre jeu de données) */
proc forest data=mycas.junkmail inmodel=mycas.forest_model;
output out=mycas.score_later predicted_class;
run;
/* Afficher les premières observations des données scorées */
proc print data=mycas.score_later (obs=10);
run;
1
caslib _all_ assign;
2
3
/* Charger le jeu de données Sashelp.JunkMail dans une table CAS temporaire */
4
DATA mycas.junkmail;
5
SET sashelp.junkmail;
6
RUN;
7
8
/* Entraîner un modèle Forest et le sauvegarder (première exécution) */
/* Afficher les premières observations des données scorées */
28
PROC PRINTDATA=mycas.score_later (obs=10);
29
RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
« In the high-performance environment of SAS Viya, efficiently managing the transition from model training to production scoring is a technical priority. The OUTMODEL= and INMODEL= options in PROC FOREST provide a native, table-based mechanism to persist and reuse your Random Forest logic across different sessions or datasets. »
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.