<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/sas-to-r-migration-for-financial-data-lessons-and-examples/"></div></div><div><a data-url="https://www.r-bloggers.com/sas-to-r-migration-for-financial-data-lessons-and-examples/" data-counturl="https://www.r-bloggers.com/sas-to-r-migration-for-financial-data-lessons-and-examples/" data-count="horizontal" data-text="SAS to R Migration for Financial Data: Lessons and Examples" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.revolutionanalytics.com/2016/11/sas-to-r-migration.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p><em>by Lixun Zhang (Data Scientist), Ye Xing (Senior Data Scientist) and Tao Wu (Principal Data Scientist Manager), all at Microsoft</em></p>
<blockquote>
<p><strong>Editor's Note</strong>: To learn more about migrating from SAS to R, there will be a live webinar presented by Lixun and Ye tomorrow (Tuesday, November 15). <a target="_blank" rel="nofollow" href="https://info.microsoft.com/sas-to-r-migration-registration.html">Register to attend the webinar here</a>.</p>
</blockquote>
<p>R has been gaining in popularity among data professionals in recent years in industries such as financial services, as shown for example in this survey from executive search firm <a target="_blank" rel="nofollow" href="http://www.burtchworks.com/2016/07/13/sas-r-python-survey-2016-tool-analytics-pros-prefer/">Burtch Works</a>. In this blog post we share some key considerations in migrating SAS to R for a financial services workload. More specifically, we will focus on the data manipulation aspects of the migration.</p>
<p>One of the most important differences between SAS and R is how data are processed. Take the process of calculating the sum of two variables as an example, which is shown by the SAS and R code below.</p>
<p><a target="_blank" rel="nofollow" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01bb09516fe3970d-pi"><img title="SAStoR" alt="SAStoR" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01bb09516fe3970d-800wi" border="0"></a></p>
<p>SAS processes data row by row by using an implied loop in Data Step. The following graph shows how it execute the operation on a dataset with 3 rows. It starts by calculating the sum for row 1, write the result of row 1 to output table, then do the calculation for row 2 and repeat this till the end of the dataset. Assuming the data have been sorted by the variable &quot;x&quot;, SAS also records that row 1 is the first occurrence of x = 10 and row 2 is the last occurrence of x = 10 by the &quot;first.&quot; and &quot;last.&quot; statement, respectively. This can be useful in situations where only the first or last occurrence should be kept in the output dataset.</p>
<p><a target="_blank" rel="nofollow" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d238407d970c-pi"><img title="Sas processes data" alt="Sas processes data" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d238407d970c-800wi" border="0"></a></p>
<p>R, on the other hand, applies functions at the column level by processing all rows at the same time, as shown in the following graph. Since R processes data by column, it does not have a corresponding function for SAS &quot;first.&quot; or &quot;last.&quot;</p>
<p><a target="_blank" rel="nofollow" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b7c8ae718d970b-pi"><img title="R processes data" alt="R processes data" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b7c8ae718d970b-800wi" border="0"></a></p>
<p>Because of the differences between SAS and R, one of the best approaches for converting SAS programs to R is to first understand what a block of SAS programs is doing and then rewrite the code in R. To illustrate this, we summarized several scenarios and published them into Cortana Intelligence Gallery as 3 Jupyter notebooks so that you can test out the R code in <a target="_blank" rel="nofollow" href="https://studio.azureml.net/">Azure Machine Learning Studio</a>. These notebooks cover some common business scenarios in financial industry such as counting delinquencies by account and calculating total expense by account. Some important technical concepts such as SAS “retain”, “first.”, and “last.” statements and R’s <span>apply()</span> and <span>sapply()</span> functions are demonstrated in these samples.</p>
<p>To run the notebooks, you can start by clicking on <a target="_blank" rel="nofollow" href="http://gallery.cortanaintelligence.com/Notebook/SAS-to-R-Tutorial-Part-1-1">SAS to R Tutorial</a> and then click on “Open in Studio.”</p>
<p>Cortana Intelligence Gallery: <a target="_blank" rel="nofollow" href="https://gallery.cortanaintelligence.com//Notebook/SAS-to-R-Tutorial-Part-1-1">SAS to R Tutorial Part 1</a></p>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/sas-to-r-migration-for-financial-data-lessons-and-examples/"></div></div><div><a data-url="https://www.r-bloggers.com/sas-to-r-migration-for-financial-data-lessons-and-examples/" data-counturl="https://www.r-bloggers.com/sas-to-r-migration-for-financial-data-lessons-and-examples/" data-count="vertical" data-text="SAS to R Migration for Financial Data: Lessons and Examples" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://blog.revolutionanalytics.com/2016/11/sas-to-r-migration.html"> Revolutions</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/iJiq5Gfyu0U"></a></div>