BookmarkSubscribeRSS Feed

Getting Started with the R SWAT Package in SAS Viya

Started ‎03-15-2021 by
Modified ‎05-14-2021 by
Views 5,798
Paper 1142-2021
Author:   

Brian Varney, Experis Solutions

Abstract

SAS Viya(r) gives SAS(r) and R developers the ability to collaborate and work off the same data sitting in memory on a SAS Viya server. This paper intends to show how to set up the connection from RStudio and process data using SAS Cloud Analytic Services (CAS). Examples will also be shown comparing the execution of analytics using R Studio and SAS Viya.

 

Introduction

SWAT stands for SAS Wrapper for Analytics Transfer. This package enables you to connect from R to a SAS Cloud Analytic Services host, run actions on in-memory tables, and work with the results of the actions. The purpose of this paper is to help SAS Viya users that have a basic understanding of R get started in using this technology. 

 

What is needed to get started with the r swat package

The following is needed to get started using the R SWAT Package with SAS Viya:

  • A 64 bit SAS Viya programming-only environment either in Windows or Linux. A full SAS Viya deployment will also work. You must be on SAS Viya 3.1 or later.
  • A 64 bit R environment either on Linux or Windows. I would highly recommend using RStudio® as the development environment.  
  • The ability for the SAS Viya and R environment servers to communicate with each other. Specifically, the R environment will need to be able to authenticate into the SAS Viya environment.
  • The R SWAT Package downloaded, installed, loaded in the R environment.
  • The dplyr, jsonlite, and httr packages installed and loaded in the R environment.
  • A basic understanding of coding in R.


Getting sas viya ready to accept a connection

An active CAS session must be run on the SAS Viya server for R to connect to. Included below are a few lines of code that will start up a cas session for R to connect to.

/* initiate a CAS server connection listening for connection requests */
options cashost="localhost" casport=5570;
cas;

cas mySession sessopts=(caslib=casuser timeout=1800 locale="en_US");

 

Methods to connect to SAS viya Using the R Swat package

As far as authentication goes, it is recommended to set up an authinfo file. Otherwise, you will need to supply your credentials in your connection code.

 

Authinfo File Documentation

https://documentation.sas.com/?docsetId=authinfo&docsetTarget=n0xo6z7e98y63dn1fj0g9l2j7oyq.htm&docse... 

 

There are two different methods for connecting to SAS Viya: binary and rest communication.

 

Binary communication

This can only be used if your R environment is in Linux. An excerpt from the SAS documentation below shows advantages and disadvantages of using binary communication.

An example of a binary connection is:

conn_binary <- CAS("cloud.example.com", 5570)

There are no credentials in the connection definition above so an authinfo file is assumed to be set up. The following is an example of a binary connection with the credentials embedded.

conn_binary <- CAS('localhost', 5570, username="<username>", password="<password>")
BrianVarney_0-1618501441608.png

Rest communication

This will work if your R environment is in Linux or Windows. An excerpt from the SAS documentation below shows advantages and disadvantages of using rest communication.

Following is an example of a rest connection:

conn_binary <- CAS('localhost', 8777, protocol='http')

There are no credentials in the connection definition so an authinfo file is assumed to be set up. The following is an example of a rest connection with the credentials embedded.

conn_rest <- CAS('localhost', 8777, protocol='http', username="<username>",password="<password>")
BrianVarney_1-1618501589106.png

 

The rest of the examples in this paper will leverage the binary connection. Submit the binary connection line of code below in R. The code and console results are shown below.

# R Code to connect to SAS Viya CAS
conn_binary <- CAS('localhost', 8777, protocol='http')
conn_binary
R Console excerpt: NOTE: Connecting to CAS and generating CAS action functions for loaded action sets... NOTE: To generate the functions with signatures (for tab completion), set options(cas.gen.function.sig=TRUE). conn_binary
CAS(hostname=localhost, port=5570, username=bvarney, session=44f86478-d497-bd49-bc21-2a7782ee2494, protocol=cas)

 

understanding CAS, CASL. actions, and Action sets

To effectively write code, we must first have a basic understanding of the mechanisms necessary to interact with SAS Viya from R. A brief explanation of the components follow:

 

  • CAS stands for Cloud Analytics Services. It is a cloud-based run-time environment for data management and analytics in SAS Viya.
  • CASL stands for Cloud Analytics Services Language. This is a language that can be used by SAS via PROC CAS or by other clients that can interact with CAS such as R, Python, & Lua. CASL is used to run code in CAS.
  • Actions are single tasks in CAS.
  • Action Sets are actions that are grouped together based on common functionality.

When we use the SAS R SWAT Package, it allows us to run processes in the SAS Viya CAS server from R using the functions from the action sets.

Many function names in CASL are typically constructed using the convention cas.<action set>.<action>(). 

 

For example, there is an action set for “Tables”. It contains actions such as “recordCount”. It would be called from R using syntax such as:

 

cas.table.recordCount(conn_binary, table='HMEQ')

The above code would return the following result to the RStudio console.  

BrianVarney_2-1618501881174.png

 

 The documentation for the action sets can be found at:

 

SAS® Viya® 3.5 Actions and Action Sets by Name and Product

https://documentation.sas.com/?cdcId=pgmcdc&cdcVersion=8.11&docsetId=allprodsactions&docsetTarget=ti...

 

sharing data

The connected R session can access the data on the SAS Viya server’s CAS session. The following SAS Viya code loads a SAS Data Set into the SAS Viya server’s memory. The promote option is important to allow the R session to be able to access the data.

libname mycaslib cas caslib=casuser;
proc casutil; load data=sampsio.hmeq casout="hmeq" outcaslib=casuser promote; run;
SAS Viya Log Excerpt

72         libname mycaslib cas caslib=casuser;
 NOTE: Libref MYCASLIB was successfully assigned as follows: 
       Engine:        CAS 
       Physical Name: 2d7ff5c6-82a0-f04c-9b75-2187e4ca23f9
73          proc casutil;
  NOTE: The UUID '2d7ff5c6-82a0-f04c-9b75-2187e4ca23f9' is connected using session MYSESSION.
 74             load data=sampsio.hmeq casout="hmeq" outcaslib=casuser promote;
NOTE: SAMPSIO.HMEQ was successfully added to the "CASUSER" caslib as "hmeq".
 75           run;

The HMEQ Data Set in the MYCASLIB SAS Library

BrianVarney_0-1618508827439.png

 

Accessing the SAS Viya Data from R

Now that the HMEQ data is sitting in the memory of the SAS Viya server and we have already established the connection from R, we can run a cas.table.tableinfo() function to list the contents of the SAS CAS library. The output is wrapped but you should be able to see the HMEQ table in there with 5,960 rows, 13 columns, etc.

 

Showing Available Data Sets in SAS Viya CAS Library

cas.table.tableInfo(connection_binary)
$TableInfo Name Rows Columns IndexedColumns Encoding CreateTimeFormatted ModTimeFormatted AccessTimeFormatted 1 HMEQ 5960 13 0 utf-8 2020-09-22T16:48:09-04:00 2020-09-22T16:48:09-04:00 2020-09-22T16:59:56-04:00 JavaCharSet CreateTime ModTime AccessTime Global Repeated View SourceName SourceCaslib Compressed Creator Modifier 1 UTF8 1916426889 1916426889 1916427596 1 0 0 0 bvarney SourceModTimeFormatted SourceModTime 1 NaN

 

Using HMEQ from the SAS Viya CAS Library

The command below sets up an R Object that is a pointer and can be accessed as you would an R data frame for some R functions.

 

hmeq_fromcas <- defCasTable(conn_binary, "HMEQ")
BrianVarney_1-1618509008418.png

The command below downloads the data into R List Object

 

hmeq_fromcas1 <- to.casDataFrame(hmeq_fromcas)

 

BrianVarney_2-1618509076914.png

The command below downloads the data into an R Data Frame Object. 

hmeq_fromcas2 <- data.frame(to.casDataFrame(hmeq_fromcas))

 

Figure 1.JPG

 

Using R summary() function on the downloaded data

summary(hmeq_fromcas2)
BrianVarney_3-1618509203079.png

 

Similarly in SAS Viya

proc summary data=mycaslib.hmeq print min q1 median mean q3 max;
   var _numeric_;
run;
proc freq data=mycaslib.hmeq;
   table _character_;
run;

Yields the following output like we produced in R.

 

BrianVarney_4-1618509290925.png

 

BrianVarney_5-1618509318502.png

 

 

Loading R Data Frames to SAS Viya cas library

If you have data in R and would like to upload it to a SAS Viya CAS library, you can use the as.casTable() function.

mtcars_cas <- as.casTable(conn_binary, mtcars, 
                          casOut =list(name="mtcars_cas", promote=TRUE))
BrianVarney_6-1618509422841.png

 

 

Loading Action Sets that are not Loaded by Default

 

loadActionSet(conn_binary, 'decisionTree')
BrianVarney_7-1618509497604.png

 

Conclusions

You should now have a basic understanding of how the R SWAT package works and build off of the examples shown in this paper.

Using the R SWAT package with SAS Viya gives a user the flexibility of using the R programming language on data sitting in a SAS Viya CAS library. This will allow SAS and R developers to collaborate more easily using the same data sources.

Contact Information

Your comments and questions are valued and encouraged. Contact the author at:

Brian Varney

Experis Solutions

269-365-1755

brian.varney@experis.com

 

References

Getting Started with SAS® Viya® for R 

 

SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration.

Other brand and product names are trademarks of their respective companies.

Comments

I'm wondering if there will be a PROC R just like PROC PYTHON?

Hi @Atabarut,

Thanks for visiting the SAS Community, reading the article, and posting your question. I'd recommend posting this question in a thread on the SAS Procedures board. Please make sure to reference this article.

 

Thanks,

Joe

Version history
Last update:
‎05-14-2021 10:05 AM
Updated by:
Contributors

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!

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Article Labels
Article Tags