BookmarkSubscribeRSS Feed

Additional Steps in Coding with SAS VIYA and CAS

Started ‎05-09-2017 by
Modified ‎03-28-2019 by
Views 4,222

The open source interfaces in SAS Viya are deservedly receiving a lot of attention. The ability to connect from Python, Lua, Java and the REST API to a CAS server for data preparation, exploration and analysis on in-memory data is some pretty cool stuff. Sneaking in under the radar is another interface that hasn't received quite as much attention PROC CAS.


PROC CAS is the SAS procedural interface to the CAS server. Just like the open source interfaces you can use it to connect and perform actions against a CAS server. The basic syntax of PROC CAS will be familiar to anyone who used PROC IMSTAT in SAS 9.4. A CAS action, in the format actionset.action, is followed by a / and then any action options.




Let's look at a simple example. In this example we will:


  • Add a CASLIB
  • Read data from a CASLIB into memory
  • List the columns in the data
  • View a few rows of the data
  • Perform a simple analysis to get the cardinality of the character variables.
  • Perform a simple summary and store the results in a SAS dataset


In the first step we will start a CAS session and then add a CASLIB. We will not discuss CASLIBS and sessions here, If you need to know what a CASLIB is view this video.


The CAS statement connects to the session then the:


  • table.addcaslib adds a caslib of the type path which points to a directory on the file system (by default this is a session caslib).
  • table.loadtable loads the cars.sashdat file from the caslib to an in-memory CAS table called cars.


cas &sysuserid._ses ;
proc cas;
session &sysuserid._ses ;
table.addcaslib / path="/admin/admindata" name="mycaslib" datasource={srctype="path"};
table.loadtable / caslib="mycaslib" path="cars.sashdat" casout={name="cars" replace=true};



In the next step the:


  • table.columinfo provides details of the columns in the loaded table.
  • table.fetch prints 10 rows from the table for three of the columns.


table.columninfo / table="cars"; run;
table.fetch / table= {name="cars",vars="MAKE","MODEL","MSRP"}} to=10;





Finally we can perform some simple analysis on the table:


  • simple.distinct calculates the cardinality of four of the character variables
  • simple.summary summarize the data and stores the summary in the result table sumres
  • saveresult takes the summary result table (sumres) and outputs it to a SAS dataset in the local SAS session


simple.distinct / table={name="cars"} inputs={"MAKE","MODEL","TYPE","ORIGIN"}; run;
simple.summary result=sumres /table={name="cars"};
saveresult sumres dataout=work.sumres;





This is a simple example which will be familiar to those who have previously used PROC IMSTAT. The summary data now in the SAS session could be manipulated, printed, graphed etc.  In addition to outputting the result table to BASE SAS, most actions also provide the capability to output it to a CAS table.


Lets look at a more complex example. One of the most powerful features of PROC CAS is that it includes a scripting language called CASL. In the next PROC CAS example we will read the results of an action and pass information to a subsequent action. The code:


  • performs the fileinfo action to list all the files available in the active CASLIB and store the results.
  • reads the results table to get a list of files available in the CASLIB.
  • loops through the list and loads each source file into memory.


proc cas;session &sysuserid._ses ;
table.fileinfo result=fileresult / caslib="mycaslib";
do cvalue over filelist;
   print (;
   table.loadtable /
         caslib="mycaslib" casout={name=scan(,1,'.') replace=true};


Lets look at each step in a little more detail. The result= option on the fileonfo tells the action to store the CAS result table in a variable fileresult.


The CAS result table is a table that is created as the result of an action. In addition to rows and columns, the table also contains labels and variable types. The table is the primary means to return information to CASL. The print statement prints the result table.




 The describe displays the contents of the fileresult variable in the SAS log. The output shows that the variable is a dictionary with 1 item which is a table. The table has 5 rows and 7 columns.




Findtable returns the table from the results to the variable filelist.


The code then uses a do over to loop through filelist and return the name of the source files. The name is then passed to a loadtable action to load the table into memory. The in-memory table name is created from the source file name using the scan function to remove the file extension.


The result in the log shows all tables in the source path of the CASLIB dynamically loaded into memory to a table with the same name as the source file.  




 Of course if your preference is Python, LUA or Java all this could also be achieved using those programming tools. If you want to see the last example in python I have included it below.



import swat
sess = swat.CAS('',5570)

caslib=sess.table.addcaslib(name="mycaslib", path="/admin/admindata/",datasource={"srctype":"PATH"},activeonadd="True")


# Loop thru the file names and load to memory
for fname in filelist:
     print ("Loading ", fname,' to ',tname)

rc = sess.session.endSession( ) 


This blog has just scratched the surface of what you can do with PROC CAS. I hope it will encourage you the explore the procedure


A whole new world. Very interesting. Thank you.

You are welcome ChrisNZ. Thanks for taking the time to respond, it is indeed new and exciting.   

Version history
Last update:
‎03-28-2019 02:10 PM
Updated by:



Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Tags