Skip to Content

R2SAS

Matthew Flynn's picture

R2SAS

-Matthew J. Flynn, Ph.D.-
mattflynn@me.com

R in business

My application of R in business is a method to aid two groups of R users at our company. The first group is composed of experienced SAS modelers who are experimenting with R statistical packages. These packages offer new statistical techniques that are not currently offered in SAS. The second group is made up of new hires that have a strong R background but need access to company data on in order to be productive. This data is on UNIX (AIX & Linux) SAS servers & variety of databases. R is currently only available on desktop PCs. In order for either group to use R, they must use a method that is manually based, time intensive, and error-prone.

The current process requires a user to undergo a multiple step process: first, they must run the job in SAS on the company servers, possibly accessing databases, then massage and summarize that data. Next, a user must write the data out of SAS into a “.csv” text files, manually transfer or download that data to the desktop, and finally import it into R via read.csv(). Then the usual data exploration, visualization and modeling can take place. To make use of the model results, that manual data transfer process is then repeated, in reverse.

In contrast, this new application of R allows R users to access the SAS servers directly and simply. Traditional SAS clients such as SAS Enterprise Guide (SAS/EG), the SAS Add-in for Microsoft Office (SAS/AMO), and the desktop JMP package use the underlying architecture of SAS Integration Technologies. This technique we will tap directly into those components using R.

Connecting SAS (& JMP) to R

SAS has recently added the capabilities to reach out & run R very easily from within both SAS/IML and JMP.

JMP 9 added R Connectivity – adding a set of functions to initiate and connect to a background R session running on the user’s Windows PC. Here is one example of the R Connection functions, from the JMP Help menu, under Scripting Index that can be run from the help index or copied to a JMP script window.

RConnection = R Connect();
RConnection <<
Submit("
x <- rnorm(1000)
hx <- hist(x, breaks=100, plot=FALSE)
plot(hx, col=ifelse(abs(hx$breaks) < 1.669, 4, 2))
x <- rnorm (100)
y <- x**2 + rnorm (100)
summary(y)
");
Wait( 3 );
RConnection << Disconnect;

This produces the expected output from R:

TKIntRJMP.R version 5.04
Min. 1st Qu. Median Mean 3rd Qu. Max.
-2.4760 -0.2130 0.6008 0.7441 1.3730 6.3360

This may also be accomplished through SAS/IML. An example of this follows:


submit / R;
rx <- matrix( 1:3, nrow=1) # vector of sequence 1,2,3
rm <- matrix( 1:9, nrow=3, byrow=TRUE) # 3x3 matrix
rq <- rm %*% t(rx) # matrix multiplication
print(rq)
endsubmit;

run ExportDataSetToR("Sashelp.Class", "df" );
submit / R;
names(df);
endsubmit;

Either of these techniques, used within SAS, will allow the user to access the functionality of R; when these scripts are invoked in SAS the computer opens a session of R in the background, executes the embedded R code, and returns the results. This can also be accomplished from the other direction.

Connecting R to SAS (& JMP)

While some current R packages allow the user to transfer SAS data or SAS transport data sets to R, there was previously no method to embed SAS in R. I have compiled a package called “R2SAS” that accomplishes this very thing. R2SAS is a collection of functions in R which initiate and connect to background SAS and foreground or background JMP sessions to execute SAS code, JMP functions, or even JSL, JMP Scripting Language code. These functions are convenient wrappers around primary connectivity functionality provided by the RDCOMClient package in R. Additionally SAS Integration Technologies, a free download in SAS, allows the user to connect to remote SAS sessions. Here is an example from an R session:


>library(R2SAS);
>ls()
[1] "JMPConnect" "JMPGetData" "JMPGetDataCol"
[4] "JMPNewdatatable" "JMPOpendoc" "JMPOpendt"
[7] "JMPQuit" "JMPRunCommand" "JMPRunJSLFile"
[10] "JMPSendData" "JMPSendDataFile" "JMPSendGraphics"
[13] "JMPSubmit" "JMPSubmitJSLFile" "SASAssignLibRefs"
[16] "SASConnect" "SASDisconnect" "SASExportData"
[19] "SASImportData" "SASSubmit" "SASSubmitFile"

Getting Started with R2SAS

In order to use R2SAS effectively, one must establish a connection from R to a background SAS session. This beginning step is accomplished with the SASConnect() function, which makes the connection using MS COM. An example call using SASConnect() is as easy as:

>SASConnect("local")

We can now use the next part of the package, SASSubmit(), to embed SAS code in the R Session.

> sascode <- '
%let n= 500;
libname temp "C:/temp";
data temp.aa;
do x = 1 to &n.;
y = rannor(1234);
output;
end;
run;
proc print data=temp.aa;
title "n=&n.";
run;
';
SASSubmit(sascode)

We can also execute existing SAS files from R. These files can be read from disk with SASSubmitFile(), with the option to pass parameters, as one would with a SAS stored process.

>SASSubmitFile("file:C:/temp", "test", "obs=18", log=TRUE);

Kind of odd to see SAS output imbedded within R isn’t it? We can also assign SAS data libraries.

And, data generated in SAS dataset can be returned back to R via SASImportData() as an R data frame.

Connecting R to remote SAS Servers.

SAS Integration Technologies provides the missing link that allows the user to connect to remote UNIX and Linux SAS servers. In this example, data generated in R can be pushed to the remote SAS server as a SAS dataset.


>SASConnect("remote_UNIX_server")

># generate some data
>nobs <- 1505
>xx<-rnorm(nobs)
>df <- data.frame(xx)
>class(df); dim(df); head(df)

># send it to SAS
>SASExportData( "tcsasnu1", "home", "newtable", "df", "xx", n=1505 );
># report on the data frame passed to remote server as a SAS dataset
>sascode <- "
libname home '/sasusers/mjflynn';
proc contents data=home.newtable; run;
proc means data=home.newtable;
run;
proc print data=home.newtable(obs=6);
format xx 12.8;
run;
";
>SASSubmit(sascode, log=TRUE)

There are examples using Visual basic that allow a user to download SAS data sets from a server to local basic; this example allows the user to transfer data in the opposite direction, a heretofore undocumented and unsupported process.

R2SAS – The Package

The package, soon to appear on the R CRAN database, contains everything a user needs to invoke SAS from R, to access remote SAS databases, and even to access SAS/JMP from R. This allows users to tie together R and SAS packages from the R side, giving users access to R-exclusive packages and the more familiar R environment while still allowing access to critical SAS data. Interested readers are encouraged to send an email to mattflynn@me.com. Feedback and other comments are appreciated.

Comments

lowell.it1's picture

Our flagship link building service – Paint It White. Links from manually created beautiful web 2.0 posts on premium blogs. Supported by an array of tier 2 and 3 links for maximum ranking increases
link building service