Brian Varney, Experis Solutions
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.
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.
The following is needed to get started using the R SWAT Package with SAS Viya:
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");
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.
There are two different methods for connecting to SAS Viya: binary and rest 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>")
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>")
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)
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:
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.
The documentation for the action sets can be found at:
SAS® Viya® 3.5 Actions and Action Sets by Name and Product
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
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.
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
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")
The command below downloads the data into R List Object
hmeq_fromcas1 <- to.casDataFrame(hmeq_fromcas)
The command below downloads the data into an R Data Frame Object.
hmeq_fromcas2 <- data.frame(to.casDataFrame(hmeq_fromcas))
Using R summary() function on the downloaded data
summary(hmeq_fromcas2)
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.
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))
loadActionSet(conn_binary, 'decisionTree')
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.
Your comments and questions are valued and encouraged. Contact the author at:
Brian Varney
Experis Solutions
269-365-1755
brian.varney@experis.com
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.
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
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Ready to level-up your skills? Choose your own adventure.