BookmarkSubscribeRSS Feed

Additional Steps in Coding with SAS VIYA and CAS

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

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.

 

gnn_proccas.png

 

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};
run;

 

gnn_proccas_1.png  

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;
run;
 

gnn_proccas_2.png

 

 

 

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;
run;
quit;
 

gnn_proccas_4.png

 

gnn_proccas_3.png  

 

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";
print(fileresult);
describe(fileresult);
 
filelist=findtable(fileresult);
do cvalue over filelist;
 
   print (cvalue.name);
   table.loadtable /
         caslib="mycaslib" path=cvalue.name casout={name=scan(cvalue.name,1,'.') replace=true};
end;
quit;  

 

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.

 

gnn_proccas_5.png

 

 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.

 

gnn_proccas_6.png

 

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.  

 

gnn_proccas_7.png

 

 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

Comments

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:

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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