SAS VIYA Guide

SAS Viya Tutorial: Everything About the table.tableInfo Action

L'équipe WeAreCAS 19/02/2024 20 vistas

In the SAS© Viya environment, in-memory data management is crucial. Before manipulating a table, it is often necessary to check its existence, know its size, or retrieve its metadata (creation date, encoding, number of rows).

This is where the tableInfo action from the table action set comes in.

In this tutorial, we will explore how to use this essential command for data exploration, from its basic syntax to advanced wildcard character management.


What is the tableInfo action?

The tableInfo action allows you to obtain detailed metadata about one or more tables loaded in memory in a specific caslib.

Key parameters:

  • caslib: The name of the CAS library where the table is located.

  • name: The name of the target table.

  • quiet: A boolean (True/False). If enabled, it suppresses error messages if the table does not exist (very useful for automated scripts).

  • wildIgnore: Determines whether special characters (% and _) should be treated as wildcards or as literal text.

  • wildEscape: Defines the escape character to force a literal reading of a wildcard.

Note :
Use Case 1: Basic Usage
Objective: Check the information of a specific table.

In this first example, we will load a small table into memory and request its information. This is the most standard use case: you know the name of the table and its location.
1cas;
2 
3/* 1. Création d'une table temporaire en mémoire CAS */
4DATA casuser.ma_table_basique;
5 INPUT ID Name $ Value;
6 DATALINES;
7 1 Alice 100
8 2 Bob 150
9 3 Charlie 120
10;
11RUN;
12 
13/* 2. Afficher les informations de la table */
14PROC CAS;
15 TABLE.tableInfo RESULT=r /
16 name="ma_table_basique"
17 caslib="casuser";
18 PRINT r;
19RUN;
First, we create a table `ma_table_basique` in the personal caslib `casuser`. Then, the `table.tableInfo` action is called. The result (stored in the variable `r`) will contain columns like Name, Rows, Columns, and CreateTime.
Note :
Use Case 2: Handling Non-Existent Tables (Quiet Option)
Objective: Prevent your program from crashing if a table is missing.

When automating processes, a table might not be loaded yet. By default, SAS© would return an error. The `quiet` option allows you to handle this gracefully.
1cas;
2 
3/* Création d'une table de référence */
4DATA samples.produits_ventes;
5 INPUT Annee Produit $ Quantite Prix;
6 DATALINES;
7 2023 A 10 100
8 2023 B 20 50
9;
10RUN;
11 
12PROC CAS;
13 /* Cas A : Table introuvable avec quiet=TRUE */
14 TABLE.tableInfo RESULT=r_non_existent /
15 name="table_introuvable"
16 quiet=TRUE;
17
18 PRINT "Tentative sur table inexistante :";
19 PRINT r_non_existent;
20 
21 /* Cas B : Table existante standard */
22 TABLE.tableInfo RESULT=r_existent /
23 name="produits_ventes"
24 caslib="Samples";
25
26 PRINT "Tentative sur table existante :";
27 PRINT r_existent;
28RUN;
In Case A, we are looking for `table_introuvable`. Thanks to `quiet=TRUE`, the log will not display a blocking red error. The result object will be empty or indicate an 'OK' status but with no table data, which allows your code to continue its execution (for example, to load the missing table).
Note :
Use Case 3: Using Wildcards
Objective: List all tables that follow a certain pattern.

This is a very powerful feature for exploration. For example, to process all sales tables for a given year.
1cas;
2 
3/* Création de plusieurs tables pour l'exemple */
4DATA casuser.donnees_2023_Q1; ID=1; RUN;
5DATA casuser.donnees_2023_Q2; ID=2; RUN;
6DATA casuser.donnees_2024_Q1; ID=3; RUN;
7 
8PROC CAS;
9 /* Rechercher toutes les tables commençant par "donnees_2023_" */
10 TABLE.tableInfo RESULT=r_wildcard /
11 name="donnees_2023_%" /* Le motif recherché */
12 caslib="casuser"
13 wildIgnore=FALSE; /* Active l'interprétation du '%' */
14
15 PRINT r_wildcard;
16RUN;
Here, `wildIgnore=FALSE` is essential. It tells CAS: "Do not treat the % character as a letter, but as a wildcard." The action will therefore return the metadata for `donnees_2023_Q1` and `donnees_2023_Q2`, but will ignore the one from 2024.
Note :
Use Case 4: Escaping Special Characters
Objective: Find a table whose name actually contains a % or a _.

This is a rarer but technical case. If your table is named `resultat_final_%_test`, how do you search for it without the % being seen as a wildcard? You need to use an escape character via `wildEscape`.
1cas;
2 
3/* Création d'une table avec un nom complexe */
4DATA casuser."resultat_final_%_test";
5 ID=10; Score=95.5;
6RUN;
7 
8PROC CAS;
9 TABLE.tableInfo RESULT=r_escape /
10 name="resultat_final_\%_test" /* Le \ protège le % */
11 caslib="casuser"
12 wildIgnore=FALSE /* On garde le mode wildcard actif */
13 wildEscape="\\"; /* On définit \ comme le protecteur */
14
15 PRINT r_escape;
16RUN;
In this example:

`wildIgnore=FALSE`: The wildcard mode is active (necessary to use `wildEscape`).

`wildEscape="\\"`: We define the backslash as the escape character. (Note: two backslashes are used because in a SAS© string, the backslash itself must be escaped).

`name="...\%..."`: The backslash before the percent sign tells CAS to search for the literal % character, not to use it to replace any text.

The tableInfo action is much more than a simple verification command. Mastering its quiet and wildIgnore parameters allows you to build robust and dynamic ETL (Extract, Transform, Load) scripts in SAS© Viya, capable of adapting to changing data environments.