<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/sasmap-static-code-analysis-for-sas/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.mango-solutions.com/wp/2017/03/sasmap-static-code-analysis-for-sas/"> Mango Solutions » R Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Ava Yang</p>
<p>You may drop your weapons, this is not going to be about SAS vs R. If you work with a large amount of SAS legacy code, <b>sasMap</b>, an R package with a <b>Shiny</b> app, is for you. It evolved from our experience in migrating SAS to R, see Mark Sellor’s <a target="_blank" rel="nofollow" href="http://www.mango-solutions.com/wp/2017/02/production-r-at-ons/">post</a> about production R at ONS for an example.</p>
<p><em>Disclaimer</em>: there’s no such thing as SAS-to-R auto translator, yet. sasMap is a map that can keep you on track by making it easy to look for a path through a wild land.</p>
<h2>Overview</h2>
<hr>
<p>Often multiple macros are nested to construct main SAS analyses. User macros are held in sub-folders and are called in top level scripts. <b>sasMap</b> calculates summary statistics of SAS scripts and helps to understand macro and script dependency. The key functionalities of the package are:</p>
<ul>
<li>Extract summary statistics such as procs and data steps</li>
<li>Draw a barplot of proc calls</li>
<li>Visualize static and interactive network of script dependency</li>
</ul>
<p>And to accomplish this the package provides the following functions:</p>
<ul>
<li><b>parseSASscript</b> Parse a SAS script</li>
<li><b>parseSASfolder</b> Parse a SAS folder</li>
<li><b>listProcs</b> List frequency of various proc calls</li>
<li><b>drawProcs</b> Draw frequency of various proc calls in a bar plot</li>
<li><b>plotSASmap</b> Draw script dependency in static plot</li>
<li><b>plotSASmapJS</b> Draw script dependency in interactive way</li>
</ul>
<h2>Demo</h2>
<hr>
<p>The package includes some dummy SAS code in the \examples\SAScode\Macros folder. The folder contains one high level script <em>MainAnalysis.SAS</em> and a subfolder called <em>Macros</em> where the user’s macros live. The main assumption is that each macro corresponds to a script of the same name. Some macros are called but don’t have a named script. For example, %summary in <em>Util2.SAS</em>, is not displayed in the static network representation, whereas it belongs to internal macros group in the interactive network graph.</p>
<p>The summary statistics include measures such as number of lines (nLines), Procs, number of data step (Data_step), macro calls (Macro_call) and macro defined (Macro_define).</p>
<pre>
# Install sasMap from github
devtools::install_github(&quot;MangoTheCat/sasMap&quot;)

# Load library
library(sasMap)

# Navigate to target directory
sasDir

# Parse SAS folder
kable(parseSASfolder(sasDir))
</pre>
<p><img alt="table1" class="wp-image-6693" src="https://i1.wp.com/www.mango-solutions.com/wp/wp-content/uploads/2017/03/table1.png?w=450" data-recalc-dims="1"></p>
<pre>
# Draw frequency of proc calls
drawProcs(sasDir)
</pre>
<p><img alt="unnamed-chunk-1-1" class="wp-image-6675" src="https://i0.wp.com/www.mango-solutions.com/wp/wp-content/uploads/2017/03/unnamed-chunk-1-1.png?w=450" data-recalc-dims="1"></p>
<pre>
# Draw network of SAS scripts. A pdf file can be created by specifying the file name.
net &lt;- renderNetwork(sasDir)
# plotSASmap(net, width=10, height=10, pdffile='sasMap.pdf') 
plotSASmap(net, width=10, height=10)
</pre>
<p><img alt="unnamed-chunk-2-1" class="wp-image-6676" src="https://i1.wp.com/www.mango-solutions.com/wp/wp-content/uploads/2017/03/unnamed-chunk-2-1.png?w=450" data-recalc-dims="1"></p>
<pre>
## Alternatively, draw it interactively (not run here)
plotSASmapJS(sasDir)
</pre>
<h2>Put them together</h2>
<hr>
<p>The <b>sasMap</b> package is accompanied by a shiny app which you can run by executing the following line of code:</p>
<pre>
library(shiny)
runApp(system.file('shiny', package='sasMap'))
</pre>
<p>Once the “I want to specify a local directory (Warning: It only works when running the shiny app from a local machine).” box is ticked, exposed is a “Choose directory” button which makes it straightforward to direct to your SAS folder (thanks to the shinyFiles package). You can also view a demo version of the app <a target="_blank" rel="nofollow" href="https://mangothecat.shinyapps.io/sasmap/">here</a>. For demo’s purpose, the deployed version has the dummy SAS code hard-coded.</p>
<h2>Conclusion</h2>
<hr>
<p>At Mango we have benefited greatly from this way of working with SAS code (see this <a target="_blank" rel="nofollow" href="http://www.mango-solutions.com/wp/2017/03/goodbye-sas-hello-r-why-you-need-to-make-the-switch-and-how-mango-can-help/">blogpost</a> for more information). If you want to know more about the <b>sasMap</b> package or about SAS to R migration feel free to contact us by phone (+44 (0)1249 705 450) or mail (<a target="_blank" rel="nofollow" href="mailto:sales@mango-solutions.com">sales@mango-solutions.com</a>). The code for this post is available on <a target="_blank" rel="nofollow" href="https://github.com/MangoTheCat/blog_sasmap">github</a> as is the code for the <a target="_blank" rel="nofollow" href="https://github.com/MangoTheCat/sasMap">package</a>.<br>
 </p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.mango-solutions.com/wp/2017/03/sasmap-static-code-analysis-for-sas/"> Mango Solutions » R Blog</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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/VMjKzxaTEv0"></a></div>