BookmarkSubscribeRSS Feed

Custom Task Tuesday: CAS Actions in a Custom Task

Started ‎09-04-2018 by
Modified ‎11-13-2018 by
Views 1,415

This task uses CAS actions that allow the user to view information about their chosen data set. A CAS Action sends a request to the server, invokes the action function, and returns the result. Viya PROCs are built from CAS Actions, but you can also code in Viya by writing the actions yourself using PROC CAS.medium.jpg

 

The task will accept a CAS table as input. The user can choose what output they are interested in from the options of table info, column info, or a simple data summary. The 3 CAS actions used are tables.tableinfo, tables.columninfo, and simple.summary.

 

Here's what the task looks like, with all output options selected and controls filled out:

 

1.PNG

Here is the PROC CAS code that is generated after selecting the above options: 

proc cas;
	table.tableinfo /name='CARS' caslib='CASUSER';
	table.columninfo result=r /table={name='CARS', caslib='CASUSER'};
	print r;
	simple.summary / table={name='CARS', caslib='CASUSER' , groupby={'Make'} } 
		casOut={caslib="CASUSER", name="MYDATA", replace=true};
	run;

proc print data=CASUSER.MYDATA;
run;

 

Finally, here are the results, that come from running the task:2.PNG

 

 

Parsing Libref and Name for Output CAS Table

One important point to note when using CAS actions in a Custom Task is that CAS Actions typically separate table name and library name, while the Custom Task controls output one long string for a two-level name (libraryname.datasetname). To use this with a CAS action, you will need to parse out the library name and the table name to fill in the CAS action. This can be done with the following VTL code:

 

#set($outputLibrefIndex = $outputData.toString().indexOf(".")) 
#set($outputLibrefIndex1 = $outputData.toString().indexOf(".")+1)   
#set($outputCASLibref = $outputData.toString().substring(0, $outputLibrefIndex))
#set($outputCASName = $outputData.toString().substring($outputLibrefIndex1))

#set($inputLibrefIndex = $DATASOURCE.toString().indexOf(".")) 
#set($inputLibrefIndex1 = $DATASOURCE.toString().indexOf(".")+1)   
#set($inputCASLibref = $DATASOURCE.toString().substring(0, $inputLibrefIndex))
#set($inputCASName = $DATASOURCE.toString().substring($inputLibrefIndex1))

Once you have pulled out the table and libref names into VTL macro variables ($outputCASName, $outputCASLibref, and $inputCASName, $inputCASLibref), you can reference them in your SAS Code.

 

Here is the rest of the VTL Code Template SAS code (but don't forget, you can download the task from our GitHub to try it yourself and see the entire CTM file):

 

proc cas ;
#if ( $chkTABLE == 1 )
	table.tableinfo /name='$inputCASName' caslib='$inputCASLibref';
#end

#if ( $chkCOLUMN == 1 )
	table.columninfo result=r /table={name='$inputCASName', caslib='$inputCASLibref'};
	print r;
#end

#if ( $chkSUMMARY == 1 )
simple.summary / table={name='$inputCASName', caslib='$inputCASLibref' #if ($VAR.size() !=0 ), groupby={ #foreach( $item in $VAR ) '$item' #end} #end}
	casOut={caslib="$outputCASLibref", name="$outputCASName", replace=true};	
#end
run;

#if ( $chkSUMMARY == 1 )
proc print data=$outputData;
run;
#end

 

Do you have any ideas to improve this Custom Task? Let me know in the comments below!

 

Join the Conversation on Twitter

Use the hashtag #CustomTaskTuesday and tweet Twitter_bird_logo_2012.svg.png@OliviaJWright with your Custom Task comments and questions!

 

Want to try it yourself?

Visit our SAS Studio GitHub to download the code for this task and follow along. 

 Take Me to GitHub!

Version history
Last update:
‎11-13-2018 12:46 PM
Updated by:

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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