Many of the examples in the Python series of articles here use a CASTable object to invoke actions or apply DataFrame-like syntax to CAS tables. In those examples, the CASTable object is generally the result of an action that loads the CAS table from a data file or other data source. But what if you have a CAS table already loaded in a session and you want to create a new CASTable object that points to it?
The first thing you need is a connection to CAS.
In[1]: import swat
In[2]: conn = swat.CAS(host, port, username, password)
We'll load a table of data here so we have something to work with. We'll also specify a table name and CASLib so they are easier to reference in the following step.
In[3]: conn.read_csv('https://raw.githubusercontent.com/sassoftware/sas-viya-programming/master/data/class.csv',
casout=dict(name='class', caslib='casuser'))
Out[3]: CASTable('class', caslib='CASUSER(kesmit)')
Using the tableinfo action, we can see that the table exists, however, we didn't store the output of the read_csv method, so we don't have a CASTable object pointing to it.
In[4]: conn.table.tableinfo(caslib='casuser')
The solution is fairly simple, you use the CASTable method of the CAS connection object. You just pass it the name of the table and the name of the CASLib just as it is printed in In[2] above.
In[5]: cls = conn.CASTable('class', caslib='CASUSER')
In[6]: cls
Out[6]: CASTable('class', caslib='CASUSER')
We now have a CASTable object that we can use to interact with.
In[7]: cls.to_frame()
In[8]: conn.close()
You can download the Jupyter notebook version of this article at https://github.com/sassoftware/sas-viya-programming/tree/master/communities.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.