Data Access SAS VIYA CAS

Bridge the Gap: Reading and Writing Excel Files Directly from the CAS Server

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

Expert Advice

Simon
Expert SAS et fondateur.

When exporting data back to Excel using PROC CASUTIL (as shown in your third example), the file is saved to the CAS Server's filesystem, not your local PC. If you are using SAS Studio via a web browser, you cannot simply browse to C:\MyDocuments. You must ensure the caslib points to a shared network path that both the CAS server and your local machine can access, or use the SAS Studio "Explorer" pane to download the generated .xlsx file after the save action completes.

This connector is essential for integrating data from desktop applications directly into the high-performance environment of SAS© Viya. It handles data type conversion and allows users to specify options for reading (e.g., specific sheet, cell range) or writing (e.g., file replacement, appending to an existing sheet). Access to PC files is done via a CAS session, often using the CASUTIL procedure or the CAS library.
Data Analysis

Type : CREATION_INTERNE


Examples use generated data (datalines) to simulate importing PC files or creating small datasets for export. For actual import, Excel/Access files are assumed to exist.

1 Code Block
PROC CASUTIL Data
Explanation :
This example shows how to load an Excel file (here simulated by a CSV then loaded into CAS) into a CAS table called 'ma_table_excel'. It uses `PROC CASUTIL` with the `LOAD` option and specifies the input and output caslib. The table is then promoted to be visible to all users of the CAS session.
Copied!
1/* Préparation : Créer un fichier Excel factice sur le système de fichiers du contrôleur CAS ou un chemin accessible */
2/* Dans un environnement réel, ce fichier 'mon_fichier.xlsx' existerait déjà. */
3/* Pour la démonstration, nous allons simuler son contenu avec DATA étape. */
4 
5/* Créer un fichier CSV temporaire pour simuler le contenu d'un fichier Excel */
6/* En pratique, vous auriez un fichier Excel existant */
7filename mydata temp;
8DATA _null_;
9 file mydata dlm=',' lrecl=200;
10 put 'ID,Nom,Valeur';
11 put '1,Alpha,100';
12 put '2,Beta,150';
13 put '3,Gamma,200';
14RUN;
15 
16/* Charger le fichier CSV dans une table CAS qui sera ensuite 'enregistrée' comme Excel */
17/* Ce n'est qu'une simulation. Normalement, l'Excel existerait déjà. */
18PROC CAS;
19 SESSION casauto;
20 TABLE.loadTable /
21 path='mydata.csv'
22 caslib='casuser'
23 promote=TRUE
24 fileType='CSV'
25 importOptions={getNames=TRUE};
26QUIT;
27 
28/* Utiliser PROC CASUTIL pour charger un fichier Excel (simulé) dans une table CAS */
29/* Supposons que 'mon_fichier.xlsx' est disponible dans le caslib 'public' */
30/* (chemin réel: /cas/data/public/mon_fichier.xlsx) */
31/* Dans cet exemple, nous allons utiliser le fichier CSV temporaire comme source */
32/* En réalité, le fichier d'entrée serait 'mon_fichier.xlsx' */
33 
34PROC CASUTIL;
35 load casdata="mydata.csv" incaslib="casuser" outcaslib="casuser" casout="ma_table_excel" replace;
36 promote casdata="ma_table_excel" incaslib="casuser";
37RUN;
38 
39PROC PRINT DATA=casuser.ma_table_excel;
40RUN;
41 
2 Code Block
PROC CASUTIL Data
Explanation :
This example illustrates how to refine the import of an Excel file by specifying options. Normally, options like `sheet=` (to select a specific sheet) and `range=` (for a cell range) would be used in the `importoptions` of `PROC CASUTIL`. Here, for demonstration, we simulate reading specific data.
Copied!
1/* Préparation : Créer un fichier Excel factice avec plusieurs feuilles pour démonstration */
2/* En pratique, le fichier Excel 'ventes.xlsx' existerait déjà avec ces feuilles. */
3filename xlfile temp;
4DATA _null_;
5 file xlfile dlm=',' lrecl=200;
6 /* Feuille 1: Ventes_Q1 */
7 put 'ID_Produit,Region,Ventes_Trim1';
8 put 'A1,Est,1000';
9 put 'A2,Ouest,1200';
10 /* Feuille 2: Ventes_Q2 */
11 put 'ID_Produit,Region,Ventes_Trim2';
12 put 'A1,Est,1100';
13 put 'A3,Nord,900';
14RUN;
15 
16/* Charger le fichier Excel (simulé par CSV) spécifiant une feuille et une plage */
17/* Ici, nous allons simuler un chargement à partir d'un fichier Excel (XLFILE) */
18/* en utilisant la fonctionnalité d'importation de fichiers PC. */
19/* Dans un cas réel, le XLFILE serait un vrai fichier Excel. */
20 
21PROC CASUTIL;
22 /* Supposons que xlfile contient les données des deux feuilles */
23 /* Nous allons charger la 'deuxième' feuille (Ventes_Q2) et une plage spécifique */
24 load casdata="xlfile.csv" incaslib="casuser" outcaslib="casuser" casout="ventes_q2_specifiques" replace
25 importoptions=(fileType='CSV' getNames=true);
26 promote casdata="ventes_q2_specifiques" incaslib="casuser";
27RUN;
28 
29/* Pour les fichiers Excel, les options sheet= et range= seraient utilisées comme ceci: */
30/* load casdata="ventes.xlsx" incaslib="public" outcaslib="casuser" casout="ventes_q2_specifiques" replace
31 importoptions=(fileType='XLSX' sheet="Ventes_Q2" range="A2:C3"); */
32/* Ici, nous montrons comment le connecteur interpréterait des options similaires pour la démonstration */
33 
34PROC PRINT DATA=casuser.ventes_q2_specifiques;
35RUN;
36 
3 Code Block
PROC CASUTIL Data
Explanation :
This example shows the export of an existing CAS table ('produits') to an Excel file named 'rapport_produits.xlsx'. The file is saved in the specified caslib ('casuser') and a specific sheet ('Inventaire') is defined for export. The `replace` option allows overwriting the file if it already exists.
Copied!
1/* Créer une table CAS pour l'exportation */
2DATA casuser.produits;
3 INFILE DATALINES dlm=',';
4 INPUT ID $ NomProduit $ Prix Quantite;
5 DATALINES;
6 P001,Laptop,1200,10
7 P002,Souris,25,50
8 P003,Clavier,75,30
9 ;
10RUN;
11 
12PROC CAS;
13 SESSION casauto;
14 TABLE.promote / name='produits' caslib='casuser';
15QUIT;
16 
17/* Exporter la table CAS 'produits' vers un fichier Excel 'rapport_produits.xlsx' */
18/* Le fichier sera créé dans le caslib 'casuser' (chemin réel sur le contrôleur CAS) */
19PROC CASUTIL;
20 save casdata="produits" incaslib="casuser"
21 outfile="rapport_produits.xlsx" outcaslib="casuser" replace
22 exportoptions=(fileType='XLSX' sheet="Inventaire");
23RUN;
24 
25/* Vérification (en listant les fichiers du caslib) */
26PROC CASUTIL;
27 list files caslib="casuser";
28RUN;
29 
4 Code Block
PROC CAS; PROC FEDSQL Data
Explanation :
This advanced example simulates the processing of data initially stored in an Excel file, once it has been loaded into a CAS table. It uses `PROC FEDSQL` to filter orders with the status 'Terminée' and `simple.summary` (a CAS action) to calculate the total amount of these orders. This demonstrates CAS's ability to perform high-performance analysis on data from PC files.
Copied!
1/* Préparation : Charger un fichier Excel (simulé par une table CAS) dans CAS */
2/* Nous allons créer une table CAS directement pour simuler des données issues d'Excel. */
3/* En réalité, ceci viendrait d'un 'LOAD CASDATA="mon_fichier.xlsx" ...' */
4DATA casuser.commandes_excel_simule;
5 INFILE DATALINES dlm=',';
6 INPUT ID_Commande $ DateCommande :MMDDYY10. Montant Commande_Statut $;
7 FORMAT DateCommande date9.;
8 DATALINES;
9 C001,01/15/2025,150.50,Terminée
10 C002,02/20/2025,300.00,En cours
11 C003,03/01/2025,75.25,Terminée
12 C004,03/05/2025,210.00,Annulée
13 ;
14RUN;
15 
16PROC CAS;
17 SESSION casauto;
18 TABLE.promote / name='commandes_excel_simule' caslib='casuser';
19QUIT;
20 
21/* Utiliser PROC FEDSQL pour analyser les données chargées depuis Excel */
22/* Filtrer les commandes terminées et calculer le total */
23PROC FEDSQL sessref=casauto;
24 create TABLE casuser.commandes_terminees as
25 select
26 ID_Commande,
27 DateCommande,
28 Montant
29 from casuser.commandes_excel_simule
30 where Commande_Statut = 'Terminée';
31QUIT;
32 
33PROC PRINT DATA=casuser.commandes_terminees;
34RUN;
35 
36/* Calculer le montant total des commandes terminées */
37PROC CAS;
38 SESSION casauto;
39 SIMPLE.summary /
40 TABLE={name='commandes_terminees', caslib='casuser'}
41 sumVars={{name='Montant'}};
42QUIT;
43 
Pro Tip
For large Excel files, consider converting them to CSV or Parquet before loading. While the PC Files connector is convenient, it is single-threaded; for massive datasets, multi-threaded formats will load significantly faster into CAS.
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.