SAS VIYA Guide

SAS Viya & Python: Accessing a Specific Value in a CAS Table

Simon 30/04/2023 4 vistas

When using the Python interface for SAS© Viya (the SWAT package), it is common to manipulate CASTable objects. While the global display of a table or using the fetch() method to view data is simple, the precise extraction of a single value (a specific cell defined by its row and column) requires a particular syntax.

This article shows you how to target and retrieve a specific piece of data.

Illustration

The Scenario

You have established your connection to the CAS server and instantiated a CASTable object pointing to an existing table (for example, in the public library).

New Buffer RO
# Définition de l'objet CASTable
ma_table = conn.CASTable('ma_donnee', caslib='public')
~
~

You want to retrieve the value located, for example, at row 0 of the "MSRP" column. If you use a simple print(), you get a general overview, but not the isolated value that can be used in a Python variable.

The Access Method

The object returned by data retrieval actions (like fetch) or column references on a CASTable object behave similarly to pandas DataFrames.

To access a specific cell, the logic is as follows:

  1. Select the column by its name.

  2. Use the .get() method with the index of the desired row.

Code Example

Here is how to proceed to extract the value:

New Buffer RO
# 1. Définir la table (si ce n'est pas déjà fait)
flag = conn.CASTable('lalala', caslib='public')
# 2. Accéder à la valeur (Colonne 'MSRP', Ligne 0)
valeur_precise = flag['MSRP'].get(0)
# 3. Afficher le résultat
print(valeur_precise)
~
~

In this example, flag['MSRP'] isolates the data series corresponding to the column, and .get(0) retrieves the first entry (index 0). This method is effective for validating unitary data or retrieving control parameters within your automation scripts.