Published on :
CAS TELECHARGEMENT_EXTERNE_PUIS_CREATION_INTERNE

In-memory table management (Add, Save, Delete)

This code is also available in: Deutsch Español Français
Awaiting validation
The 'table.upload' action allows loading local or remote files into an in-memory CAS table. The 'table.save' action persists an in-memory table to a persistent data source (e.g., a SASHDAT file) associated with a caslib. Finally, 'table.dropTable' deletes a table from CAS memory. The script also uses 'proc http' to download a CSV file and 'pathname' to manipulate temporary file paths, ensuring the example is self-contained.
Data Analysis

Type : TELECHARGEMENT_EXTERNE_PUIS_CREATION_INTERNE


Examples download a CSV file from a SAS server or create data internally (datalines) for demonstrations.

1 Code Block
PROC CAS, DATA step Data
Explanation :
This example creates a simple CSV file using a DATA step, then loads it into an in-memory CAS table named 'MaTableSimple'. It then saves this table as 'MaTableSimpleSauvegardee' in the active caslib and finally, deletes the original table 'MaTableSimple' from CAS memory. The 'table.tableInfo' action is used to verify the existence of the tables.
Copied!
1 
2DATA _null_;
3file _webout;
4put 'Nom,Age,Ville';
5put 'Alice,30,Paris';
6put 'Bob,24,Lyon';
7put 'Charlie,35,Marseille';
8 
9RUN;
10%let data_csv_file = '/tmp/simple_data.csv';
11filename _csv_ temp filevar=_webout;
12 
13DATA _null_;
14file _csv_;
15INPUT;
16put _infile_;
17 
18RUN;
19 
20PROC CAS;
21 
22SESSION casauto;
23TABLE.upload / path=&data_csv_file. casOut={name='MaTableSimple', replace=TRUE}, importOptions={fileType='csv'};
24PRINT 'Table MaTableSimple chargée :';
25TABLE.tableInfo / name='MaTableSimple';
26TABLE.save / TABLE='MaTableSimple', name='MaTableSimpleSauvegardee', replace=TRUE;
27PRINT 'Table MaTableSimpleSauvegardee créée :';
28TABLE.tableInfo / name='MaTableSimpleSauvegardee';
29TABLE.dropTable / name='MaTableSimple';
30PRINT 'Table MaTableSimple supprimée de la mémoire :';
31TABLE.tableInfo / name='MaTableSimple';
32 
33QUIT;
34 
2 Code Block
PROC CAS
Explanation :
This example downloads an 'air.csv' file, loads it into CAS memory as 'AirData' using 'importOptions' for better variable type detection. It then saves this in-memory table as a SASHDAT file named 'AirDataSauvegardee.sashdat' in the 'CASUSER' caslib, then 'promotes' it to make it accessible to other sessions. Finally, the original 'AirData' table is deleted from memory.
Copied!
1/* Définir l'URL du fichier CSV */
2%let csv_url = 'http://support.sas.com/documentation/onlinedoc/viya/exampledatasets/air.csv';
3 
4/* Créer un fichier temporaire pour le téléchargement */
5filename _air_ temp;
6 
7/* Télécharger le fichier CSV */
8PROC HTTP method='get' url=&csv_url. out=_air_;
9RUN;
10 
11/* Obtenir le chemin du fichier temporaire */
12%let temp_air_path = %sysfunc(pathname(_air_));
13 
14PROC CAS;
15 SESSION casauto;
16 
17 /* Charger le fichier CSV avec des options d'importation spécifiques */
18 TABLE.upload /
19 path="&temp_air_path.",
20 casOut={name='AirData', replace=TRUE},
21 importOptions={fileType='csv', guessrows=1000, vartype='best'};
22 
23 PRINT 'Table AirData chargée :';
24 TABLE.tableInfo / name='AirData';
25 
26 /* Sauvegarder la table en mémoire vers un fichier SASHDAT spécifique dans la caslib active */
27 TABLE.save /
28 TABLE='AirData',
29 name='AirDataSauvegardee.sashdat',
30 caslib='CASUSER', /* Spécifier une caslib si nécessaire */
31 replace=TRUE;
32 
33 PRINT 'Table AirDataSauvegardee.sashdat créée dans CASUSER :';
34 TABLE.tableInfo / name='AirDataSauvegardee.sashdat', caslib='CASUSER';
35 
36 /* Promouvoir la table sauvegardée pour la rendre visible à d'autres sessions */
37 TABLE.promote /
38 name='AirDataSauvegardee.sashdat',
39 caslib='CASUSER';
40 
41 PRINT 'Table AirDataSauvegardee.sashdat promue :';
42 TABLE.tableInfo / name='AirDataSauvegardee.sashdat', caslib='CASUSER';
43 
44 /* Supprimer la table originale en mémoire */
45 TABLE.dropTable / name='AirData';
46 PRINT 'Table AirData supprimée de la mémoire :';
47 TABLE.tableInfo / name='AirData';
48QUIT;
49 
50/* Libérer le fichier temporaire */
51filename _air_ clear;
3 Code Block
DATA STEP, PROC CAS Data
Explanation :
This advanced example creates an in-memory SAS table ('Produits') with formats and labels via a DATA step. It then saves this table in SASHDAT format with 'saszpg' compression and stores it in the 'CASUSER' caslib. The original table is deleted, then the saved table is reloaded into memory under a new name ('ProduitsRecharges') to demonstrate the complete cycle. The first few rows of the reloaded table are displayed for verification.
Copied!
1/* Création d'une table en mémoire via un DATA step */
2DATA casuser.Produits;
3 LENGTH Categorie $10 Produit $20;
4 INFILE DATALINES dsd;
5 INPUT Categorie $ Produit $ Prix Stock;
6 FORMAT Prix dollar8.2;
7 label Categorie='Catégorie de Produit' Produit='Nom du Produit' Prix='Prix Unitaire' Stock='Quantité en Stock';
8 DATALINES;
9Aliments,Pommes,1.50,100
10Aliments,Poires,2.00,75
11Boissons,Jus d'orange,3.25,50
12Boissons,Eau,1.00,200
13Electronique,Souris,25.99,30
14Electronique,Clavier,75.00,20
15;
16run;
17 
18proc cas;
19 session casauto;
20 
21 print 'TABLE Produits créée en mémoire :';
22 table.tableInfo / name='Produits', caslib='CASUSER';
23 
24 /* Sauvegarde de la table en mémoire avec compression et un chemin spécifique */
25 table.save /
26 table={name='Produits', caslib='CASUSER'},
27 name='ProduitsComp.sashdat',
28 caslib='CASUSER',
29 compression='saszpg', /* Compression pour économiser l'espace */
30 replace=TRUE;
31 
32 PRINT 'Table ProduitsComp.sashdat sauvegardée avec compression :';
33 TABLE.tableInfo / name='ProduitsComp.sashdat', caslib='CASUSER';
34 
35 /* Supprimer la table originale en mémoire */
36 TABLE.dropTable / name='Produits', caslib='CASUSER';
37 
38 PRINT 'Table Produits supprimée de la mémoire :';
39 TABLE.tableInfo / name='Produits', caslib='CASUSER';
40 
41 /* Recharger la table sauvegardée dans une nouvelle table en mémoire */
42 TABLE.loadTable /
43 caslib='CASUSER',
44 path='ProduitsComp.sashdat',
45 casOut={name='ProduitsRecharges', replace=TRUE};
46 
47 PRINT 'Table ProduitsRecharges rechargée en mémoire :';
48 TABLE.tableInfo / name='ProduitsRecharges', caslib='CASUSER';
49 
50 /* Afficher quelques lignes pour vérifier */
51 TABLE.fetch /
52 TABLE={name='ProduitsRecharges', caslib='CASUSER'},
53 to=5;
54QUIT;
4 Code Block
PROC CAS Data
Explanation :
This example demonstrates advanced CAS operations. After creating and loading a temporary table, it saves it to the 'CASUSER' caslib. It then uses the 'session.info' action to get details about the current CAS session and 'builtins.listCaslibs' to list all available caslibs, which is useful for resource management and debugging. Finally, the temporary tables are deleted.
Copied!
1/* Créer une table temporaire pour la démonstration */
2DATA _null_;
3 file _webout;
4 put 'ID,Valeur';
5 put '1,10';
6 put '2,20';
7 put '3,30';
8RUN;
9%let temp_data_file = %sysfunc(pathname(temp));
10filename _tmp_data_ temp filevar=_webout;
11DATA _null_;
12 file _tmp_data_;
13 INPUT;
14 put _infile_;
15RUN;
16 
17PROC CAS;
18 SESSION casauto;
19 
20 /* Charger la table */
21 TABLE.upload /
22 path="&temp_data_file.",
23 casOut={name='TempTable', replace=TRUE},
24 importOptions={fileType='csv'};
25 PRINT 'Table TempTable chargée :';
26 TABLE.tableInfo / name='TempTable';
27 
28 /* Sauvegarder la table dans une caslib spécifique (par exemple, un chemin de fichier) */
29 /* Assurez-vous que 'caslib_perso' est une caslib existante et accessible avec des droits d'écriture */
30 /* Par exemple, 'CASUSER' est souvent disponible */
31 TABLE.save /
32 TABLE='TempTable',
33 name='TempTable_Saved',
34 caslib='CASUSER',
35 replace=TRUE;
36 
37 PRINT 'Table TempTable_Saved sauvegardée dans CASUSER :';
38 TABLE.tableInfo / name='TempTable_Saved', caslib='CASUSER';
39 
40 /* Vérifier l'état de la session CAS */
41 SESSION.info RESULT=sinfo;
42 PRINT 'Informations sur la session CAS:';
43 PRINT sinfo;
44 
45 /* Lister toutes les caslibs disponibles */
46 BUILTINS.listCaslibs RESULT=caslibs;
47 PRINT 'Caslibs disponibles:';
48 PRINT caslibs;
49 
50 /* Supprimer les tables temporaires créées */
51 TABLE.dropTable / name='TempTable';
52 TABLE.dropTable / name='TempTable_Saved', caslib='CASUSER';
53QUIT;
54 
55filename _tmp_data_ clear;
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