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!
data _null_; file _webout; put 'Nom,Age,Ville'; put 'Alice,30,Paris'; put 'Bob,24,Lyon'; put 'Charlie,35,Marseille'; run; %let data_csv_file = '/tmp/simple_data.csv'; filename _csv_ temp filevar=_webout; data _null_; file _csv_; input; put _infile_; run; proc cas; session casauto; table.upload / path=&data_csv_file. casOut={name='MaTableSimple', replace=TRUE}, importOptions={fileType='csv'}; print 'Table MaTableSimple chargée :'; table.tableInfo / name='MaTableSimple'; table.save / table='MaTableSimple', name='MaTableSimpleSauvegardee', replace=TRUE; print 'Table MaTableSimpleSauvegardee créée :'; table.tableInfo / name='MaTableSimpleSauvegardee'; table.dropTable / name='MaTableSimple'; print 'Table MaTableSimple supprimée de la mémoire :'; table.tableInfo / name='MaTableSimple'; quit;
PRINT'Table MaTableSimple supprimée de la mémoire :';
31
TABLE.tableInfo / name='MaTableSimple';
32
33
QUIT;
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!
/* Définir l'URL du fichier CSV */
%let csv_url = 'http://support.sas.com/documentation/onlinedoc/viya/exampledatasets/air.csv';
/* Créer un fichier temporaire pour le téléchargement */
filename _air_ temp;
/* Télécharger le fichier CSV */
proc http method='get' url=&csv_url. out=_air_;
run;
/* Obtenir le chemin du fichier temporaire */
%let temp_air_path = %sysfunc(pathname(_air_));
proc cas;
session casauto;
/* Charger le fichier CSV avec des options d'importation spécifiques */
table.upload /
path="&temp_air_path.",
casOut={name='AirData', replace=TRUE},
importOptions={fileType='csv', guessrows=1000, vartype='best'};
print 'Table AirData chargée :';
table.tableInfo / name='AirData';
/* Sauvegarder la table en mémoire vers un fichier SASHDAT spécifique dans la caslib active */
table.save /
table='AirData',
name='AirDataSauvegardee.sashdat',
caslib='CASUSER', /* Spécifier une caslib si nécessaire */
replace=TRUE;
print 'Table AirDataSauvegardee.sashdat créée dans CASUSER :';
table.tableInfo / name='AirDataSauvegardee.sashdat', caslib='CASUSER';
/* Promouvoir la table sauvegardée pour la rendre visible à d'autres sessions */
table.promote /
name='AirDataSauvegardee.sashdat',
caslib='CASUSER';
print 'Table AirDataSauvegardee.sashdat promue :';
table.tableInfo / name='AirDataSauvegardee.sashdat', caslib='CASUSER';
/* Supprimer la table originale en mémoire */
table.dropTable / name='AirData';
print 'Table AirData supprimée de la mémoire :';
table.tableInfo / name='AirData';
quit;
/* Libérer le fichier temporaire */
filename _air_ clear;
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!
/* Création d'une table en mémoire via un DATA step */
data casuser.Produits;
length Categorie $10 Produit $20;
infile datalines dsd;
input Categorie $ Produit $ Prix Stock;
format Prix dollar8.2;
label Categorie='Catégorie de Produit' Produit='Nom du Produit' Prix='Prix Unitaire' Stock='Quantité en Stock';
datalines;
Aliments,Pommes,1.50,100
Aliments,Poires,2.00,75
Boissons,Jus d'orange,3.25,50
Boissons,Eau,1.00,200
Electronique,Souris,25.99,30
Electronique,Clavier,75.00,20
;
run;
proc cas;
session casauto;
print 'Table Produits créée en mémoire :';
table.tableInfo / name='Produits', caslib='CASUSER';
/* Sauvegarde de la table en mémoire avec compression et un chemin spécifique */
table.save /
table={name='Produits', caslib='CASUSER'},
name='ProduitsComp.sashdat',
caslib='CASUSER',
compression='saszpg', /* Compression pour économiser l'espace */
replace=TRUE;
print 'Table ProduitsComp.sashdat sauvegardée avec compression :';
table.tableInfo / name='ProduitsComp.sashdat', caslib='CASUSER';
/* Supprimer la table originale en mémoire */
table.dropTable / name='Produits', caslib='CASUSER';
print 'Table Produits supprimée de la mémoire :';
table.tableInfo / name='Produits', caslib='CASUSER';
/* Recharger la table sauvegardée dans une nouvelle table en mémoire */
table.loadTable /
caslib='CASUSER',
path='ProduitsComp.sashdat',
casOut={name='ProduitsRecharges', replace=TRUE};
print 'Table ProduitsRecharges rechargée en mémoire :';
table.tableInfo / name='ProduitsRecharges', caslib='CASUSER';
/* Afficher quelques lignes pour vérifier */
table.fetch /
table={name='ProduitsRecharges', caslib='CASUSER'},
to=5;
quit;
1
/* Création d'une table en mémoire via un DATA step */
2
DATA casuser.Produits;
3
LENGTH Categorie $10 Produit $20;
4
INFILEDATALINES 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';
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!
/* Créer une table temporaire pour la démonstration */
data _null_;
file _webout;
put 'ID,Valeur';
put '1,10';
put '2,20';
put '3,30';
run;
%let temp_data_file = %sysfunc(pathname(temp));
filename _tmp_data_ temp filevar=_webout;
data _null_;
file _tmp_data_;
input;
put _infile_;
run;
proc cas;
session casauto;
/* Charger la table */
table.upload /
path="&temp_data_file.",
casOut={name='TempTable', replace=TRUE},
importOptions={fileType='csv'};
print 'Table TempTable chargée :';
table.tableInfo / name='TempTable';
/* Sauvegarder la table dans une caslib spécifique (par exemple, un chemin de fichier) */
/* Assurez-vous que 'caslib_perso' est une caslib existante et accessible avec des droits d'écriture */
/* Par exemple, 'CASUSER' est souvent disponible */
table.save /
table='TempTable',
name='TempTable_Saved',
caslib='CASUSER',
replace=TRUE;
print 'Table TempTable_Saved sauvegardée dans CASUSER :';
table.tableInfo / name='TempTable_Saved', caslib='CASUSER';
/* Vérifier l'état de la session CAS */
session.info result=sinfo;
print 'Informations sur la session CAS:';
print sinfo;
/* Lister toutes les caslibs disponibles */
builtins.listCaslibs result=caslibs;
print 'Caslibs disponibles:';
print caslibs;
/* Supprimer les tables temporaires créées */
table.dropTable / name='TempTable';
table.dropTable / name='TempTable_Saved', caslib='CASUSER';
quit;
filename _tmp_data_ clear;
1
/* Créer une table temporaire pour la démonstration */
2
DATA _null_;
3
file _webout;
4
put 'ID,Valeur';
5
put '1,10';
6
put '2,20';
7
put '3,30';
8
RUN;
9
%let temp_data_file = %sysfunc(pathname(temp));
10
filename _tmp_data_ temp filevar=_webout;
11
DATA _null_;
12
file _tmp_data_;
13
INPUT;
14
put _infile_;
15
RUN;
16
17
PROC 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 :';
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.
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.