ETL SAS VIYA CAS

No Data Step Required: Creating Computed Columns on the Fly with table.fetch

This code is also available in: Deutsch Español
Difficulty Level
Intermediate
Published on :
Michael

Expert Advice

Michael
Responsable de l'infrastructure Viya.

Unlike PROC PRINT, which defaults to showing observation numbers, the table.fetch action often returns an _Index_ column that represents the row's position on a specific worker node, which can look random in a distributed environment. To get a cleaner output similar to a standard report, you can add the parameter index=FALSE to your statement (e.g., table.fetch / table='ventes' index=FALSE;) to hide this technical column.

To use it like a pro, remember these three best practices:

Be Resource Efficient: Always use the to= parameter to limit your results. Fetching millions of rows to your client session can cause significant latency or memory issues; stick to the first 20–100 rows for quick verification.

Leverage CAS-Side Processing: Use the where and sortBy parameters directly within the action. This ensures filtering and sorting happen in-memory within the CAS server before any data is transferred to your interface.

Enhance Readability: Take advantage of computedVars and format=true. By applying formats directly in the fetch action, you can transform raw data into business-ready insights (e.g., converting scores to "Pass/Fail" statuses) instantly without creating a new physical table.

Think of table.fetch as the PROC PRINT for the Cloud.
The table.fetch action is used to retrieve data from a CAS table. It offers parameters to specify the number of rows to return (to), filtering conditions (where), sorting (sortBy), and the ability to apply formats to the data. It is an essential tool for quick exploration and verification of data loaded into the CAS environment.
Data Analysis

Type : CREATION_INTERNE


Examples use generated data (datalines) or SASHELP tables loaded into CAS.

1 Code Block
PROC CAS / DATA STEP Data
Explanation :
This example shows the simplest use of `table.fetch` to view the first 3 rows of a table named 'ma_table' previously created in the 'casuser' caslib.
Copied!
1/* Création d'une table de démonstration en mémoire CAS */
2DATA casuser.ma_table;
3 INPUT ID Nom $ Age Score;
4 DATALINES;
51 Alice 25 85
62 Bob 30 92
73 Charlie 22 78
84 Diana 28 95
95 Eve 35 88
10;
11RUN;
12 
13PROC CAS;
14 SESSION casauto;
15 /* Afficher les 3 premières lignes de la table */
16 TABLE.fetch / TABLE='ma_table', to=3;
17RUN;
18QUIT;
Result
2 Code Block
PROC CAS / DATA STEP Data
Explanation :
This example uses the `where` parameter to filter rows where the 'Produit' column is 'A', and the `vars` parameter to select only the 'ID' and 'Quantite' columns.
Copied!
1/* Création d'une table de démonstration en mémoire CAS */
2DATA casuser.ventes;
3 INPUT ID Produit $ Quantite Prix;
4 DATALINES;
51 A 10 100
62 B 5 150
73 A 20 100
84 C 8 200
95 B 12 150
106 A 15 100
11;
12RUN;
13 
14PROC CAS;
15 SESSION casauto;
16 /* Afficher les lignes pour le produit 'A', en sélectionnant seulement 'ID' et 'Quantite' */
17 TABLE.fetch /
18 TABLE='ventes',
19 where='Produit="A"',
20 vars={'ID', 'Quantite'};
21RUN;
22QUIT;
Result
3 Code Block
PROC CAS / DATA STEP Data
Explanation :
This advanced example shows how to sort results by 'Note' in descending order (`sortBy`), create a calculated variable `Statut` based on `Note` and formatted with a SAS format (`put(Note, note_fmt.)`). The custom format is first created with `proc format` then loaded into CAS memory with `builtins.uploadformat`.
Copied!
1/* Création d'une table de démonstration en mémoire CAS */
2DATA casuser.etudiants;
3 INPUT Nom $ Matiere $ Note;
4 DATALINES;
5Alice Math 90
6Bob Info 85
7Alice Info 92
8Charlie Math 78
9Bob Math 88
10;
11RUN;
12 
13/* Création d'un format personnalisé pour les notes */
14PROC FORMAT;
15 value note_fmt
16 low-60 = 'Échec'
17 61-75 = 'Passable'
18 76-100 = 'Excellent';
19QUIT;
20 
21PROC CAS;
22 SESSION casauto;
23 /* Charger le format en CAS */
24 BUILTINS.uploadformat /
25 caslib='casuser',
26 fmtlibname='formats';
27 
28 /* Afficher les données triées par 'Note' décroissante, avec une variable calculée et formatée */
29 TABLE.fetch /
30 TABLE='etudiants',
31 sortBy={{name='Note', order='descending'}},
32 computedVars={{name='Statut', expression='put(Note, note_fmt.)'}},
33 computedVarsProgram='data; set etudiants; run;',
34 FORMAT=true,
35 to=5;
36RUN;
37QUIT;
Result
4 Code Block
PROC CAS
Explanation :
This example demonstrates typical integration within the Viya/CAS environment. It loads a SASHELP dataset ('iris.sashdat') into CAS memory, displays general information about the loaded table via `table.tableinfo`, then retrieves and displays the first 10 rows of this CAS table with `table.fetch`. Finally, it deletes the CAS table.
Copied!
1PROC CAS;
2 SESSION casauto;
3 
4 /* Charger la table IRIS de SASHELP dans la caslib 'casuser' */
5 TABLE.loadtable /
6 caslib='casuser',
7 path='iris.sashdat',
8 casout={name='IRIS_CAS', replace=true};
9 
10 /* Afficher les informations de la table chargée */
11 TABLE.tableinfo / TABLE='IRIS_CAS';
12 
13 /* Afficher les 10 premières lignes de la table chargée */
14 TABLE.fetch / TABLE='IRIS_CAS', to=10;
15 
16 /* Nettoyer la table chargée après utilisation (optionnel) */
17 TABLE.droptable / caslib='casuser', name='IRIS_CAS';
18RUN;
19QUIT;
Result
Pro Tip
Without a sortBy parameter, the order of rows returned is non-deterministic. Since data flows in simultaneously from multiple worker nodes, running the same fetch twice might show rows in a different order.
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.