**Adventures in Statistical Computing**, and kindly contributed to R-bloggers)

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:

-SET SASAUTOS (

“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”

)

The SAS file for the macro can be downloaded here.

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

submit;

%get_stocks(spy,25MAY2010,);

endsubmit;

Next, let’s create 2 vectors, X and Y. Make Y and linear function of X.

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;

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.

submit /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;

Produces

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:

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.

**leave a comment**for the author, please follow the link and comment on his blog:

**Adventures in Statistical Computing**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...