SAS VIYA Guide

SAS Viya & Python: How to Replace a Global Table?

Simon 30/07/2023 4 vistas

Automating data flows between Python and SAS© Viya using the SWAT interface is a common practice. However, a specific error often occurs when attempting to update data shared with all users: "Global Table cannot be replaced".

This article explains why this error occurs and the best practice for updating your global tables without changing their name.

Illustration

Here is a proposal for a technical article based on the provided exchanges, written for a developer blog or an internal knowledge base.


SAS© Viya & Python: How to Replace a Global Table?

Automating data flows between Python and SAS© Viya using the SWAT interface is a common practice. However, a specific error often occurs when attempting to update data shared with all users: "Global Table cannot be replaced".

This article explains why this error occurs and the best practice for updating your global tables without changing their name.

The Problem: The Impossible Overwrite

Imagine the following scenario: you have a Python script that loads a CSV file, transforms it, and attempts to upload it to the CAS server to make it accessible to everyone (Promote to Global Scope).

Your code probably looks like this:

New Buffer RO
conn.upload('data.csv',
casout=dict(name='ma_table_partagee',
caslib='public',
promote=True,
replace=True))
~
~

On the first execution, everything works. But on the second execution, the script fails with the message:

ERROR: Global tables cannot be replaced.

Why?

In the CAS architecture, "Global" tables (those that survive the closing of your session and are visible to everyone) are protected. The promote=True and replace=True options cannot be used simultaneously to overwrite an existing global table. The system refuses this operation to prevent concurrent access conflicts or accidental deletion of critical data.

The Bad Ideas

Faced with this blockage, one is sometimes tempted to:

  1. Do not replace (replace=False): This will fail because the table already exists.

  2. Change the target table name: This works technically, but it's unmanageable for reports or users who expect to find a table named ma_table_partagee and not ma_table_v2, v3, etc.

The Solution: The "Drop & Load" Pattern

The cleanest and most robust method for updating a global table while keeping its name is to proceed in two explicit steps: delete the old version, then load/promote the new one.

Here is how to implement this cleanly in Python (SWAT):

Step 1: Delete the existing table

Use the dropTable action. The trick is to ignore errors if the table does not yet exist (for example, during the very first run) using a try/except block or a quiet parameter.

Step 2: Load and Promote the new data

Once the space is freed up, you can load your file and promote it immediately.

Complete Code Example

New Buffer RO
# Nom de la table et librairie cible
table_name = 'ventes_mensuelles'
lib_name = 'public'
# 1. Nettoyage : On supprime la table globale existante
# L'option quiet=True évite une erreur si la table n'existe pas
conn.table.dropTable(name=table_name, caslib=lib_name, quiet=True)
# 2. Chargement : On upload et on promeut la nouvelle version
conn.upload_file('nouvelles_ventes.csv',
casout=dict(name=table_name,
caslib=lib_name,
promote=True))
print(f"La table {table_name} a été mise à jour avec succès.")
~
~

If you need to refresh shared data on CAS:

  1. Do not try to force an overwrite (replace=True) directly on a promoted table.

  2. First, explicitly delete the table with dropTable.

  3. Reload your data.

This approach ensures that your SAS© Visual Analytics reports or other users always point to the most recent version of the data, under the same fixed name.