Published on :
Machine Learning CREATION_INTERNE

Scoring with PROC BOOLRULE

This code is also available in: Deutsch Español
Awaiting validation
The BOOLRULE procedure is a powerful tool for extracting explicit boolean rules from a dataset. Once these rules are extracted and a model is created (often stored in a CAS astore), it is essential to be able to apply this model to new data. The scoring process involves taking a new observation, evaluating the boolean rules against its characteristics, and then producing a prediction or classification based on these rules. This is fundamental for deploying Machine Learning models in production.
Data Analysis

Type : CREATION_INTERNE


Examples use generated data (datalines) or SASHELP to ensure code autonomy.

1 Code Block
PROC ASTORE SCORE Data
Explanation :
This first example demonstrates the simplest use of scoring with a BOOLRULE model in SAS Viya.
1. **Data Creation**: A small CAS table named `casuser.score_data_basic` is created using a `DATA` step and the `DATALINES` statement. This data simulates new observations for which we want to obtain predictions.
2. **Model Simulation**: The comment indicates that for a real scenario, a BOOLRULE model (`astore`) named `my_boolrule_model` should have been previously trained and saved (e.g., in the `casuser.models` caslib). To make the example self-sufficient without full training, we assume its existence. `PROC ASTORE SCORE` uses the `OUTMODEL` parameter to specify the model to apply.
3. **Scoring**: The `PROC ASTORE` procedure is used with the `SCORE` statement. The `DATA` option points to our data table `score_data_basic`, and `OUT` specifies the output table (`casuser.scored_results_basic`) where the scoring results (including predictions) will be stored.
4. **Displaying Results**: `PROC PRINT` displays the content of the `scored_results_basic` table, allowing visualization of the generated predictions.
5. **Cleanup**: The temporary tables created in CAS are dropped using `proc cas; table.dropTable; quit;`.
Copied!
1/* Exemple 1 : Utilisation Basique du Scoring avec PROC BOOLRULE */
2/* Supposons qu'un modèle BOOLRULE nommé 'my_boolrule_model' existe déjà dans 'casuser.models'. */
3 
4/* 1. Création d'une table CAS simple pour le scoring */
5DATA casuser.score_data_basic;
6 INPUT Feature1 Feature2 Feature3;
7 DATALINES;
810 20 5
915 25 8
1012 18 6
11;
12RUN;
13 
14/* 2. Chargement du modèle astore dans la session CAS (si ce n'est pas déjà fait) */
15/* Dans une situation réelle, l'astore serait généré par PROC BOOLRULE TRAIN et chargé une fois. */
16/* Pour cet exemple, nous simulons simplement son existence et son chargement. */
17/* Le nom de l'astore et son caslib doivent correspondre à ceux du modèle entraîné. */
18/* proc astore restore=casuser.models.my_boolrule_model; run; */
19 
20/* 3. Application du modèle pour le scoring */
21PROC ASTORE;
22 score DATA=casuser.score_data_basic
23 out=casuser.scored_results_basic
24 outmodel=casuser.models.my_boolrule_model; /* Nom fictif du modèle astore */
25RUN;
26 
27/* Affichage des résultats du scoring */
28PROC PRINT DATA=casuser.scored_results_basic;
29 title "Résultats de Scoring Basique";
30RUN;
31 
32/* Nettoyage (optionnel) */
33PROC CAS;
34 TABLE.dropTable / caslib="casuser", name="score_data_basic";
35 TABLE.dropTable / caslib="casuser", name="scored_results_basic";
36QUIT;
2 Code Block
PROC ASTORE SCORE Data
Explanation :
This example expands on the scoring scenario by introducing common options to customize the output and use larger datasets.
1. **Data Loading**: The `CARS` table from the `SASHELP` library is loaded into the `public` caslib in CAS. This larger table simulates a production dataset on which scoring will be performed.
2. **Astore Model**: Similar to the previous example, the existence of an `astore` named `my_intermediate_model` in `public.models_caslib` is assumed.
3. **Scoring with Options**: The `PROC ASTORE` procedure with `SCORE` is used.
* `DATA=public.CARS`: Specifies the input table for scoring.
* `OUT=public.scored_cars_intermediate`: Names the output table.
* `OUTMODEL=public.models_caslib.my_intermediate_model`: Indicates the model to apply.
* `/ COPYVARS=(Make Model Type MSRP)`: This option is crucial and allows copying specific variables from the input table to the output table, which is useful for analyzing results.
* `PREDICT=Predicted_Target`: Renames the default prediction variable to `Predicted_Target` for better readability.
* `EVENT=1`: Used to specify the event of interest in the case of a binary or multiclass target, allowing focusing on the probability of a specific class.
4. **Displaying Results**: `PROC PRINT` displays the first 10 observations of the results table.
5. **Cleanup**: Temporary tables created are dropped.
Copied!
1/* Exemple 2 : Scoring avec options courantes */
2/* Supposons un modèle 'my_intermediate_model' dans 'public.models_caslib'. */
3/* Nous allons utiliser un ensemble de données plus grand de SASHELP et spécifier des variables de sortie. */
4 
5/* 1. Charger une table SASHELP dans CAS pour simuler de nouvelles données à scorer */
6PROC CAS;
7 load casdata="CARS" caslib="sashelp" outcaslib="public" promote;
8QUIT;
9 
10/* 2. Supposer l'existence et le chargement d'un modèle astore nommé 'my_intermediate_model' */
11/* Remplacez ceci par votre modèle astore réel et son caslib si vous l'exécutez. */
12/* proc astore restore=public.models_caslib.my_intermediate_model; run; */
13 
14/* 3. Appliquer le modèle pour le scoring avec des options de sortie spécifiques */
15PROC ASTORE;
16 score DATA=public.CARS
17 out=public.scored_cars_intermediate
18 outmodel=public.models_caslib.my_intermediate_model /
19 copyvars=(Make Model Type MSRP) /* Copier des variables spécifiques de l'entrée */
20 predict=Predicted_Target /* Spécifier le nom de la variable de prédiction */
21 event=1; /* Spécifier l'événement d'intérêt si applicable */
22RUN;
23 
24/* Affichage des 10 premières lignes des résultats du scoring */
25PROC PRINT DATA=public.scored_cars_intermediate(obs=10);
26 title "Résultats de Scoring Intermédiaire (10 premières lignes)";
27RUN;
28 
29/* Nettoyage (optionnel) */
30PROC CAS;
31 TABLE.dropTable / caslib="public", name="CARS";
32 TABLE.dropTable / caslib="public", name="scored_cars_intermediate";
33QUIT;
3 Code Block
PROC ASTORE SCORE Data
Explanation :
This example addresses a more realistic scenario where raw data must be prepared before scoring.
1. **Raw Data Creation**: A CAS table `casuser.raw_customer_data` is created with example data including missing values, simulating real customer data.
2. **Data Preparation**: A `DATA` step is used to create `casuser.prepared_customer_data` from the raw data. Simple imputations are performed for missing values (`Age`, `Gender`, `Income`, `ScoreFeature1`, `ScoreFeature2`). In a production environment, imputation rules would be derived from the model's training statistics.
3. **Astore Model**: The existence of an `astore` model (`my_advanced_model`) trained on similarly prepared data is assumed.
4. **Scoring on Prepared Data**: `PROC ASTORE SCORE` is executed on the `casuser.prepared_customer_data` table.
* `COPYVARS=(CustomerID Age Gender Income)`: Copies customer IDs and important original variables.
* `PREDICT=Churn_Risk_Score`: Gives a business name to the prediction variable.
5. **Displaying Results**: The scoring results on the prepared data are displayed.
6. **Cleanup**: Temporary tables are dropped.
Copied!
1/* Exemple 3 : Cas Avancé - Scoring avec préparation des données */
2/* Cet exemple illustre un scénario où les données de scoring nécessitent une préparation. */
3/* Nous allons simuler une étape de préparation des données (imputation simple) */
4/* avant d'appliquer un modèle BOOLRULE avancé. */
5 
6/* 1. Création d'une table CAS avec des données potentiellement manquantes ou brutes */
7DATA casuser.raw_customer_data;
8 INPUT CustomerID Age Gender $ Income ScoreFeature1 ScoreFeature2;
9 DATALINES;
10101 35 Male 50000 . 12
11102 42 Female 65000 85 .
12103 28 Female . 40000 15
13104 50 Male 90000 92 18
14105 31 . 48000 78 14
15;
16RUN;
17 
18/* 2. Préparation des données pour le scoring (e.g., imputation des valeurs manquantes) */
19/* Dans un cas réel, cette étape utiliserait des statistiques d'imputation dérivées de l'entraînement. */
20DATA casuser.prepared_customer_data;
21 SET casuser.raw_customer_data;
22 /* Imputation simple : remplacement des valeurs manquantes par la moyenne/mode ou une constante */
23 IF Age = . THEN Age = 40; /* Exemple d'imputation pour l'âge */
24 IF Gender = ' ' THEN Gender = 'Female'; /* Exemple d'imputation pour le genre */
25 IF Income = . THEN Income = 55000; /* Exemple d'imputation pour le revenu */
26 IF ScoreFeature1 = . THEN ScoreFeature1 = 80;
27 IF ScoreFeature2 = . THEN ScoreFeature2 = 15;
28RUN;
29 
30/* 3. Supposer l'existence d'un modèle astore nommé 'my_advanced_model' */
31/* Ce modèle serait entraîné sur des données préparées de manière similaire. */
32/* proc astore restore=casuser.models.my_advanced_model; run; */
33 
34/* 4. Application du modèle pour le scoring des données préparées */
35PROC ASTORE;
36 score DATA=casuser.prepared_customer_data
37 out=casuser.scored_customer_results
38 outmodel=casuser.models.my_advanced_model /
39 copyvars=(CustomerID Age Gender Income)
40 predict=Churn_Risk_Score;
41RUN;
42 
43/* Affichage des résultats du scoring */
44PROC PRINT DATA=casuser.scored_customer_results;
45 title "Résultats de Scoring Avancé avec Préparation des Données";
46RUN;
47 
48/* Nettoyage (optionnel) */
49PROC CAS;
50 TABLE.dropTable / caslib="casuser", name="raw_customer_data";
51 TABLE.dropTable / caslib="casuser", name="prepared_customer_data";
52 TABLE.dropTable / caslib="casuser", name="scored_customer_results";
53QUIT;
4 Code Block
PROC ASTORE SCORE Data
Explanation :
This example illustrates how BOOLRULE scoring specifically integrates into the distributed SAS Viya environment, focusing on large CAS tables and astore model management.
1. **Large CAS Table Creation**: The `SASHELP.CARS` table is massively duplicated 1000 times into `casuser.large_cars_data` to simulate a large volume of data typical of distributed environments. This is done to demonstrate the efficiency of distributed in-memory scoring.
2. **Simulated Astore Creation**: Since it's not possible to generate a real astore with `PROC BOOLRULE TRAIN` without complex data and preprocessing steps, a minimal `astore` (`casuser.my_viya_model`) is simulated by creating a CAS table with version information and dummy rules. **It is crucial to note that in a real case, this astore would be the result of a `PROC BOOLRULE` training and not manually created.**
3. **Distributed Scoring**: `PROC ASTORE SCORE` is used to apply the model (`outmodel=casuser.my_viya_model`) to the large table (`data=casuser.large_cars_data`). Execution occurs in a distributed manner on the CAS server, which is optimized for large data volumes.
* `COPYVARS=(Make Model Origin)`: Copies descriptive variables to contextualize the results.
4. **Displaying Results**: The first 10 rows of the `casuser.scored_large_cars` results table are displayed.
5. **Cleanup**: All temporary tables, including the simulated `astore` model, are dropped from the CAS session.
Copied!
1/* Exemple 4 : Intégration Viya/CAS - Scoring distribué */
2/* Cet exemple met en évidence l'exécution de scoring dans l'environnement CAS de SAS Viya. */
3/* Nous allons charger un modèle astore déjà existant et effectuer le scoring */
4/* sur une grande table CAS en utilisant les capacités distribuées. */
5 
6/* 1. Connexion à la session CAS (si ce n'est pas déjà fait) */
7/* cas casauto; */
8 
9/* 2. Création d'une table CAS de grande taille (simulation) */
10/* Nous allons dupliquer la table SASHELP.CARS pour simuler une grande table distribuée */
11DATA casuser.large_cars_data;
12 SET sashelp.cars;
13 DO _n_ = 1 to 1000; /* Dupliquer 1000 fois pour simuler une grande taille */
14 OUTPUT;
15 END;
16RUN;
17 
18/* 3. Chargement du modèle astore (supposé avoir été entraîné et enregistré dans CAS) */
19/* Ici, nous utiliserons l'instruction UPLOAD ou RESTORE de PROC ASTORE pour charger le modèle */
20/* si l'astore n'est pas déjà dans la session. Pour la simulation, on suppose qu'il est déjà là. */
21/* proc astore restore=casuser.models.my_viya_model; run; */
22/* Ou si le modèle est un fichier local à uploader: */
23/* proc astore; upload rstore=my_viya_model.sashdat; run; */
24 
25/* Pour cet exemple, nous allons créer un astore minimal en mémoire pour la démonstration */
26/* (Dans la réalité, ceci proviendrait d'un entraînement PROC BOOLRULE) */
27DATA casuser.my_viya_model;
28 /* Simulate a minimal astore structure for demonstration purposes */
29 LENGTH _Astore_Model_ $256 _Name_ $32 _Value_ $256;
30 _Astore_Model_ = 'my_viya_model';
31 _Name_ = 'VERSION'; _Value_ = '1.0'; OUTPUT;
32 _Name_ = 'TARGET'; _Value_ = 'Origin'; OUTPUT; /* Example target */
33 _Name_ = 'RULE_COUNT'; _Value_ = '5'; OUTPUT;
34 _Name_ = 'RULE_1'; _Value_ = 'MSRP > 30000 AND Horsepower > 200'; OUTPUT;
35 _Name_ = 'RULE_1_PRED'; _Value_ = 'Europe'; OUTPUT;
36 _Name_ = 'RULE_2'; _Value_ = 'MPG_City < 20 AND Weight > 3500'; OUTPUT;
37 _Name_ = 'RULE_2_PRED'; _Value_ = 'USA'; OUTPUT;
38 /* ... add more dummy rules as needed for a real model simulation */
39RUN;
40 
41/* NOTE: La création d'un astore 'à la main' comme ci-dessus n'est PAS la méthode habituelle. */
42/* Normalement, un astore est créé par une procédure d'entraînement comme PROC BOOLRULE. */
43/* Cette section est uniquement pour rendre l'exemple auto-suffisant sans un entraînement préalable complexe. */
44 
45/* 4. Application du modèle pour le scoring distribué sur la grande table CAS */
46PROC ASTORE;
47 score DATA=casuser.large_cars_data
48 out=casuser.scored_large_cars
49 outmodel=casuser.my_viya_model / /* Ici on utilise l'astore créé plus haut */
50 copyvars=(Make Model Origin);
51RUN;
52 
53/* Affichage des 10 premières lignes des résultats du scoring distribué */
54PROC PRINT DATA=casuser.scored_large_cars(obs=10);
55 title "Résultats de Scoring Distribué Viya/CAS (10 premières lignes)";
56RUN;
57 
58/* Nettoyage (optionnel) */
59PROC CAS;
60 TABLE.dropTable / caslib="casuser", name="large_cars_data";
61 TABLE.dropTable / caslib="casuser", name="my_viya_model"; /* Drop the dummy astore table */
62 TABLE.dropTable / caslib="casuser", name="scored_large_cars";
63 TABLE.dropTable / caslib="public", name="CARS";
64QUIT;
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.
Copyright Info : Copyright © SAS Institute Inc. All Rights Reserved