# Calling R from SAS IML Studio

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.**Adventures in Statistical Computing**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:

-SET SASAUTOS (The SAS file for the macro can be downloaded here.

“C:\Users\pazzula\Documents\My SAS Files(32)\9.3\macros”

“!SASROOT\core\sasmacro”

“!SASROOT\accelmva\sasmacro”

“!SASROOT\dmscore\sasmacro”

“!SASROOT\ets\sasmacro”

“!SASROOT\iml\sasmacro”

“!SASROOT\stat\sasmacro”

)

To submit SAS code, surround the code with “submit;” and “endsubmit;”. This piece will download data for the SPY ETF:

Next, let’s create 2 vectors, X and Y. Make Y and linear function of X.submit;%get_stocks(spy,25MAY2010,);endsubmit;

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.”x = (1:10)`;y = 1 + 3*x;e = j(10,1,0);do i=1 to10;e[i] = .5*rannor(12345);end;y = y + e;

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

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.run ExportMatrixToR(x,“x”);run ExportMatrixToR(y,“y”);run ExportDataSetToR(“returns”,“returns”);

Producessubmit /R;require(xts);require(PerformanceAnalytics);m = lm(y~x);summary(m);returns = xts(returns$spy,returns$Date);colnames(returns) = {“SPY”};chart.CumReturns(returns[,”SPY”],main=”Total Return”);table.AnnualizedReturns(returns);endsubmit;

Call:

lm(formula = y ~ x)

Residuals:

Min 1Q Median 3Q Max

-0.79782 -0.04944 0.04503 0.17198 0.33329

Coefficients:

Estimate Std. Error t value Pr(>|t|)

(Intercept) 0.85948 0.22469 3.825 0.00505 **

x 3.02539 0.03621 83.548 4.7e-13 ***

—

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1

Residual standard error: 0.3289 on 8 degrees of freedom

Multiple R-squared: 0.9989, Adjusted R-squared: 0.9987

F-statistic: 6980 on 1 and 8 DF, p-value: 4.698e-13

SPY

Annualized Return 0.1041

Annualized Std Dev 0.1956

Annualized Sharpe (Rf=0%) 0.5323

and:

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.

To

**leave a comment**for the author, please follow the link and comment on their blog:**Adventures in Statistical Computing**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.