BookmarkSubscribeRSS Feed

Scoring using Models Deployed into CAS using SAS Code: A Step-by-Step Guide

Started ‎07-09-2024 by
Modified ‎07-09-2024 by
Views 841

SAS Model Manager supports multiple publishing destinations so models can be deployed in the location and form that best fit their use case. For example, SAS Model Manager can deploy models to batch, real-time, container, and database locations. In a previous article, I discussed the CAS destination and how to score a model deployed to CAS over REST API. SAS Cloud Analytic Services provides the run-time environment for data management and analytics on SAS Viya. Thus, you will hear CAS referenced as a place to store data, but also as an analytical engine. CAS operates in-memory to provide the best performance for big data. And of course, models deployed to CAS can be scored using SAS code. In this article, we'll review the steps required to deploy a model into CAS and score new data using that model in SAS code. 

 

Step 1: Create the CAS destination

 

While a CAS destination may not be available out-of-the-box, an admin can create one in just a few steps using the SAS Viya CLI or from Environment Manager. From Environment Manager in SAS Viya, first click Publishing Destinations. Then click the folder icon in the upper-right corner to create a new destination. Select the type as CAS, give the destination a name, select the CAS server, select the CAS library, and finally give the model table a name before clicking save.

 

SophiaRowland_0-1720551245470.png

 

Step 2: Publish to CAS

 

Now that we have configured our destination, we are ready to publish our model. We can deploy a SAS, Python, or R model into CAS. First, select your model(s) and hit the Publish button. From the Publish Models dialogue, select the CAS destination. Give your published model a memorable name before clicking the Publish button.

 

SophiaRowland_1-1720551315577.png

Now we are ready to open a new SAS program in SAS Studio and start using our deployed model.

 

Step 3: Start a CAS session

 

To interact with CAS, we need to start a session. We can start a CAS session and access the available CAS libraries by running the following code:

cas; 
caslib _all_ assign;

 

Step 4: Load data into CAS

 

If your data isn't already in CAS, there are several methods for loading data into CAS. Using the Data Explorer on SAS Viya, under Manage Data in the main menu, we can build connections to a database or import a local file in just a few steps. We can also load data programmatically, with methods supporting data from various locations.  

/*****************************************************************************/
/*  Load file from a client location ("pathToClientFile") into the specified */
/*  caslib ("myCaslib") and save it as "tableNameForLoadedFile".             */
/*****************************************************************************/

proc casutil;
	load file="pathToClientFile" 
	outcaslib="myCaslib" casout="tableNameForLoadedFile";
run;

/*****************************************************************************/
/*  Load SAS data set from a Base engine library (library.tableName) into    */
/*  the specified caslib ("myCaslib") and save as "targetTableName".         */
/*****************************************************************************/

proc casutil;
	load data=library.tablename outcaslib="myCaslib"
	casout="targetTableName";
run;

/*****************************************************************************/
/*  Load a table ("sourceTableName") from the specified caslib               */
/*  ("sourceCaslib") to the target Caslib ("targetCaslib") and save it as    */
/*  "targetTableName".                                                       */
/*****************************************************************************/

proc casutil;
	load casdata="sourceTableName" incaslib="sourceCaslib" 
	outcaslib="targetCaslib" casout="targetTableName";
run;

 

Step 5: Score data

 

Now to call the model, we will use the runModelLocal action. You will need a few parameters to fill out the code below, including:

  • modelName: the name you gave your model when you published it in step 2.
  • modelTable: the name of the table and associated CASLib for the destination created in step 1.
  • inTable: the name of the table you want to score using your model.
  • outTable: a name for the scored table.
proc cas; 
   loadactionset "modelPublishing";
   runModelLocal  /                                  
      modelName="deployed_name",
      modelTable={caslib="destination_caslib", name="destination_table_name"},
      inTable={caslib="sourceCaslib", name="sourceTableName"},
      outTable={caslib="targetCaslib", name="targetTableName"};
run;
quit;

 

Step 6: Promote data

 

Our table was scored within our CAS session and as such, is not available outside our session. But, with one more call we can promote our table, so it is available globally, even after our session is gone. Update the code with your library and table names.

proc casutil;
	promote casdata="sourceTableName" incaslib="sourceCaslib" 
	outcaslib="targetCaslib" casout="targetTableName";
run;

 

Step 7: Close session

 

Our session will expire after a set amount of time passes, but we can be good stewards of our resources by closing our session once we are done.

cas casout terminate;

 

Step 8: Integrate

 

From here, you can use the scored data or the scoring code within a process that supports your use case best. For example, the scored data can be used within a dashboard or report in SAS Visual Analytics. Or you can take the code into a SAS Studio flow to string it together with other processing steps. And if you need the code scheduled, you can create a job around the code and schedule it. There are several options to fit your use case and needs to create a seamless analytics experience!

 

What to learn more about model execution in SAS Model Manager? Then check out these resources:

 

Version history
Last update:
‎07-09-2024 03:26 PM
Updated by:
Contributors

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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