CAS

Promoting CAS Tables: Understanding Scope and Avoiding the "Table not found" Error

Simon 24/05/2024 7 vistas

In SAS© Viya, sharing data between users is based on a key concept: promotion. A table loaded into memory is by default visible only to you (Session Scope). To make it public, you must promote it (Global Scope).

However, it often happens that the promote statement fails with a frustrating message: ERROR: There is no session-scope table X in caslib Public...

Yet, you are sure you loaded this table! Let's analyze why this happens and how to solve it properly with CASL.


The Problem: "Session Scope" vs. "Global Scope"

The error usually occurs due to confusion about the lifespan of a CAS session.

Look at this typical failing scenario:

  1. You load a table MyTable in a session (let's say Session A).

  2. You run a new script starting with cas MySession;.

  3. You attempt to promote MyTable.

Why does it crash? The cas MySession; statement often starts a new session (Session B). Session B is completely isolated from Session A. It cannot "see" MyTable which is in the memory of Session A. For promote, the table simply does not exist.


The Robust Solution: Do Everything in CASL

Rather than using PROC CASUTIL sequentially (which can hide session changes), the recommended approach by experts (like SASJedi) is to use a PROC CAS block. This allows managing the deletion of the old global version and the promotion of the new one in a single logical transaction.

Furthermore, depending on your Viya version, the action to use differs slightly to copy and promote a table simultaneously (for example, to rename it in the process).

Illustration
Note :
The Universal Code
Here is the optimized script that handles cleanup and promotion, taking your Viya version into account.
1PROC CAS;
2 /* 1. Nettoyage : On supprime l'ancienne version globale si elle existe */
3 /* L'option quiet=TRUE évite une erreur si la table n'existe pas encore */
4 TABLE.dropTable /
5 caslib="Public"
6 name="GL_PERIODS_Global" /* Nom final de la table publique */
7 quiet=TRUE;
8 
9 /* 2. Promotion : Copie de la table de session vers une table globale */
10
11 /* --- Option A : Pour Viya 3.4 et 3.5 --- */
12 /* On utilise table.partition comme astuce pour copier + promouvoir */
13 TABLE.partition /
14 TABLE={caslib="Public", name="GL_PERIODS_Local"} /* Table source (session) */
15 casout={caslib="Public", name="GL_PERIODS_Global", promote=TRUE};
16 
17 /* --- Option B : Pour Viya 2020.1 (Viya 4) et plus récents --- */
18 /* L'action copyTable est plus explicite et recommandée */
19 /*
20 table.copyTable /
21 table={caslib="Public", name="GL_PERIODS_Local"}
22 casout={caslib="Public", name="GL_PERIODS_Global", promote=TRUE};
23 */
24QUIT;
Why does this method work better?
Atomicity: Everything happens within the same procedure.

Renaming: Unlike a simple promote, this method (partition or copyTable) creates a new physical copy. This allows you to keep your working table (_Local) intact while publishing a clean version (_Global).

Version Management: In Viya 3.5, the table.partition action was often used in a diverted way from its primary function to perform this efficient copy-promotion.

3 Best Practices to Remember

  • One script, one session: Ensure that data loading (Load) and promotion (Promote) occur in the same execution flow or the same CAS session.

  • Check for existence: Before promoting, make sure your source table is in memory ("Loaded"). If your session has timed out, the table has disappeared from RAM.

  • Connect by UUID: If you absolutely need to retrieve a table left in an orphaned session, you must reconnect to that specific session using its UUID: cas mysess uuid="ca683ddf-fe18-3c48-a04e-45718220976d";