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:
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:
In the next step the:
Finally we can perform some simple analysis on the table:
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:
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('myhost.x.com',5570)
caslib=sess.table.addcaslib(name="mycaslib", path="/admin/admindata/",datasource={"srctype":"PATH"},activeonadd="True")
filelist=sess.table.fileinfo(caslib="mycaslib").FileInfo.Name
print(filelist)
# Loop thru the file names and load to memory
for fname in filelist:
tname=fname.split(".")[0]
print ("Loading ", fname,' to ',tname)
rc=sess.table.loadtable(caslib="mycaslib",path=fname,casout={"name":tname,"replace":"true"})
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.