CAS SAS VIYA CAS

Stop Losing Your PROC CAS Output: Master the SAVERESULT Statement

This code is also available in: Deutsch Español
Difficulty Level
Beginner
Published on :
Simon

Expert Advice

Simon
Expert SAS et fondateur.

There is a key difference between the table.save action and the SAVERESULT statement. Use table.save when you simply want to dump an existing in-memory table to disk (like a .sashdat file). However, use SAVERESULT when you need to convert a CAS Dictionary or Result Object (the variable created by an action like simple.summary result=r) into a physical table. SAVERESULT is the bridge that transforms analytical output into data you can join, filter, or export.

Use SAVERESULT to turn temporary analytical outputs into permanent data assets.
The SAVERESULT statement saves the specified table to a data source accessible by the CAS server. You must specify a CAS table and at least one destination option. Common options include CASOUT=, REPLACE, and DBASE=. The CAS table can be an in-memory table or a temporary table created by a previous action. The output format is determined by the DBASE= option or inferred from the CAS table's metadata.
Data Analysis

Type : INTERNAL_CREATION


Examples use generated data (datalines) or SASHELP.

1 Code Block
PROC CAS / DATA STEP Data
Explanation :
This example illustrates the basic use of `SAVERESULT`. It first creates a local SAS dataset, loads it into memory on the CAS server, then uses `SAVERESULT` to save this in-memory CAS table to a new CAS table named 'mycars_saved'.
Copied!
1/* Crée un jeu de données SAS local */
2DATA mylib.cars_data;
3 SET sashelp.cars;
4 where make in ('Toyota', 'Honda');
5RUN;
6 
7/* Démarre une session CAS et charge le jeu de données en mémoire */
8PROC CAS;
9 SESSION casauto;
10 caslib _all_ assign;
11 
12 /* Charge la table SAS dans CAS */
13 TABLE.loadtable /
14 caslib='casuser',
15 path='cars_data.sashdat',
16 casout={name='mycars_cas', replace=true};
17 
18 /* Sauvegarde la table CAS en mémoire dans une nouvelle table CAS */
19 saveresult mycars_cas / casout={name='mycars_saved', caslib='casuser', replace=true};
20 
21 /* Vérifie que la nouvelle table a été sauvegardée */
22 TABLE.tableinfo / caslib='casuser' name='mycars_saved';
23 
24 /* Affiche les premières lignes de la table sauvegardée */
25 TABLE.fetch / caslib='casuser' TABLE={name='mycars_saved'};
26 
27QUIT;
28 
29/* Nettoyage : Supprime les tables et le jeu de données local */
30PROC CAS;
31 TABLE.droptable / caslib='casuser' name='mycars_cas';
32 TABLE.droptable / caslib='casuser' name='mycars_saved';
33QUIT;
34 
35PROC DELETE DATA=mylib.cars_data; RUN;
2 Code Block
PROC CAS
Explanation :
This example demonstrates how to use `SAVERESULT` to save the results of a CAS action. It loads a dataset, executes the `simple.summary` action, which creates a temporary in-memory table, then uses `SAVERESULT` with the `CASOUT=` and `REPLACE` options to permanently save these results to 'hmeq_summary_saved'.
Copied!
1/* Démarre une session CAS */
2PROC CAS;
3 SESSION casauto;
4 caslib _all_ assign;
5 
6 /* Charge un jeu de données SASHELP dans CAS */
7 TABLE.loadtable /
8 caslib='casuser',
9 path='Hmeq.sashdat',
10 casout={name='hmeq_cas', replace=true};
11 
12 /* Exécute une action CAS pour obtenir des statistiques sommaires */
13 SIMPLE.summary /
14 TABLE='hmeq_cas',
15 inputs={'debtinc', 'loan'},
16 casout={name='summary_results', replace=true};
17 
18 /* Sauvegarde la table de résultats temporaire 'summary_results' */
19 saveresult summary_results / casout={name='hmeq_summary_saved', caslib='casuser', replace=true};
20 
21 /* Vérifie que la table de résultats a été sauvegardée */
22 TABLE.tableinfo / caslib='casuser' name='hmeq_summary_saved';
23 
24 /* Affiche les premières lignes de la table sauvegardée */
25 TABLE.fetch / caslib='casuser' TABLE={name='hmeq_summary_saved'};
26 
27QUIT;
28 
29/* Nettoyage : Supprime les tables CAS */
30PROC CAS;
31 TABLE.droptable / caslib='casuser' name='hmeq_cas';
32 TABLE.droptable / caslib='casuser' name='summary_results';
33 TABLE.droptable / caslib='casuser' name='hmeq_summary_saved';
34QUIT;
3 Code Block
PROC CAS / DATA STEP Data
Explanation :
This advanced example shows how to save an in-memory CAS table to an external CSV file on the CAS server's file system. It uses the `DBASE='CSV'` option to specify the format and `PATH='output/complex_data_saved.csv'` to define the output path. The `REPLACE` option is used to overwrite an existing file.
Copied!
1/* Crée un jeu de données SAS local avec des données complexes */
2DATA mylib.complex_data;
3 LENGTH id 8 name $20 value 8 date 8;
4 FORMAT date yymmdd10.;
5 INPUT id name $ value date;
6 DATALINES;
7 1 John 100 20230115
8 2 Jane 150 20230220
9 3 Mike 120 20230325
10 4 Sara 200 20230430
11 ;
12RUN;
13 
14/* Démarre une session CAS */
15PROC CAS;
16 SESSION casauto;
17 caslib _all_ assign;
18 
19 /* Charge la table SAS dans CAS */
20 TABLE.loadtable /
21 caslib='casuser',
22 path='complex_data.sashdat',
23 casout={name='complex_cas', replace=true};
24 
25 /* Sauvegarde la table CAS en un fichier CSV sur le système de fichiers CAS */
26 /* Le chemin 'path' est relatif au caslib 'casuser' */
27 saveresult complex_cas /
28 dbase='CSV',
29 path='output/complex_data_saved.csv',
30 replace=true;
31 
32 /* Vérifie l'existence du fichier CSV (cela nécessite un accès au système de fichiers CAS) */
33 /* Note: Cette vérification n'est pas directe via PROC CAS pour les fichiers externes. */
34 /* Pour vérifier, il faudrait utiliser une action qui liste les fichiers du caslib */
35 TABLE.fileinfo / caslib='casuser' path='output';
36 
37QUIT;
38 
39/* Nettoyage : Supprime les tables et le jeu de données local */
40PROC CAS;
41 TABLE.droptable / caslib='casuser' name='complex_cas';
42 /* La suppression du fichier CSV nécessite un accès direct au système de fichiers ou une action CAS spécifique */
43 /* Pour cet exemple, nous n'incluons pas de suppression de fichier externe. */
44QUIT;
45 
46PROC DELETE DATA=mylib.complex_data; RUN;
4 Code Block
PROC CAS
Explanation :
This example illustrates Viya integration by creating a filtered CAS view, then using `SAVERESULT` to save this view as a new permanent table on the CAS server. The `PROMOTE=TRUE` option is used to make the table globally available to all sessions, which is a key feature of the Viya/CAS environment.
Copied!
1/* Démarre une session CAS */
2PROC CAS;
3 SESSION casauto;
4 caslib _all_ assign;
5 
6 /* Charge le jeu de données iris de SASHELP dans CAS */
7 TABLE.loadtable /
8 caslib='casuser',
9 path='Iris.sashdat',
10 casout={name='iris_cas', replace=true};
11 
12 /* Crée une table filtrée en mémoire */
13 DATA casuser.iris_filtered (casview=true);
14 SET casuser.iris_cas;
15 IF Species = 'Setosa';
16 RUN;
17 
18 /* Sauvegarde la vue filtrée en une table permanente et la promeut */
19 /* La promotion rend la table disponible globalement pour toutes les sessions */
20 IF _OR_.exists(caslib='casuser', name='iris_setosa_promoted') THEN DO;
21 TABLE.droptable / caslib='casuser' name='iris_setosa_promoted';
22 END;
23 saveresult iris_filtered / casout={name='iris_setosa_promoted', caslib='casuser', replace=true} promote=true;
24 
25 /* Vérifie que la table a été sauvegardée et promue */
26 TABLE.tableinfo / caslib='casuser' name='iris_setosa_promoted';
27 
28 /* Affiche les premières lignes de la table sauvegardée */
29 TABLE.fetch / caslib='casuser' TABLE={name='iris_setosa_promoted'};
30 
31QUIT;
32 
33/* Nettoyage : Supprime les tables CAS */
34PROC CAS;
35 TABLE.droptable / caslib='casuser' name='iris_cas';
36 TABLE.droptable / caslib='casuser' name='iris_filtered'; /* Supprime la vue en mémoire */
37 TABLE.droptable / caslib='casuser' name='iris_setosa_promoted';
38QUIT;
Pro Tip
When saving to external files like CSV using DBASE=, remember that the path= is relative to the Caslib's physical path, not your client machine's local directory.
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.

Related Documentation

Aucune documentation spécifique pour cette catégorie.