Calling R from SAS IML Studio

I am playing around with SAS IML Studio 3.4. For those that do not know, IML (Interactive Matrix Language) is the Matlab-esk language from SAS. It opperates from normal SAS code through the PROC IML procedure. A new (to me at least) UI has been developed for analysts called IML Studio. IML Studio uses a superset of the IML language called IMLPlus. I’ll be digging into it (and the goodies like linked graphs, Java integration, and the ability to call 3rd party dll’s) later.

One of the more recent additions to the IML and IMLPlus languages is the ability to run SAS routines from within IML. At the same time this functionality was added, SAS also added the ability to call R from within IML. You can now pass IML matrices back and forth betwen R matrices and SAS Datasets back and forth to R Data Frames (and other types).

Having never done this, I fired up IML Studio and set out to learning.

First, save the macros created in my last post into the an Autocall library. You can modify the autocall libraries by modifying the sasv9.cfg file and adding the path to the SASAUTOS list. Mine looks like this:

Nothing hard about that. Those new to IML will want to know that ` is the transpose operator and “j(n,m,value)” creates a matrix (n x m) filled with “value.”

Exporting IML matrices and SAS Data Sets to R is straight forward. Use the modules ExportMatrixToR() and ExportDataSetToR().

run ExportMatrixToR(x,“x”);

run ExportMatrixToR(y,“y”);

run ExportDataSetToR(“returns”,“returns”);

The second parameter to each module is the name to give the object in R. To call R, we again use “submit” and “endsubmit,” only this time we add “/ R” to the submit line. So let’s run a linear model on y~x, create an XTS object from the returns Data Frame, chart the cumulative returns of SPY and create an AnnualizedReturn table.

The SAS Data Set contained a column called Date that had a SAS Date format applied. During the conversion to R Data Frame, SAS was nice enough to convert that column into an R date. ﻿

That’s pretty much it. It’s pretty straight forward. Personally, I’m excited about this. There are some things, like data manipulation, that SAS is way better than R at. But then there are things that R gives me that I have to work to code in SAS (like easy functions for portfolio analytics). Now I get the best of both worlds.