SAS vs R Programming: Which to Choose and How to Switch
SAS is losing its footing across industries due to the <b>rise of Shiny</b>, an R package giving users <b>bespoke interactivity</b> on top of their R routines. In this article, we’ll discuss <b>SAS vs R Programming</b> in the context of the pharmaceutical industry, but the topic of conversation applies to any data science user looking to switch <b>data analytics tooling</b>. Data science teams are searching for SAS alternatives that can better handle their technical needs while satisfying non-technical personnel with <b>interactive data storytelling</b>. The result of this search boils down to Python vs R Programming. And although there are drag-n-drop BI tools, these solutions do not satisfy <b>custom development</b>, <b>machine learning</b>, and <b>big data</b> handling needs. <blockquote><a href="https://appsilon.com/appsilon-data-science-is-now-an-rstudio-full-service-certified-partner/" target="_blank" rel="noopener">Appsilon is an RStudio (Posit) Full Service Certified Partner</a>. Find out how we can help you with R and Python development services and RStudio discounts.</blockquote> If your team uses Python and is comfortable with this language, we won’t try to evangelize you. But if you find <b>value in R for data analytics</b> and statistical analysis, we highly recommend exploring the innovations R can provide in your organization. <h3>Breaking down statistical analysis to understand R vs SAS</h3> A statistical analysis has several steps: problem statement, data collection, data wrangling, data analysis, and results-based communication. The data analysis step usually involves summarizing data using descriptive statistics and applying inferential statistics through hypothesis testing and modeling. Sharing results is usually done by writing reports. These reports commonly contain different visualizations that help contextualize the analysis; especially for those not involved throughout the analysis process. To perform this kind of analysis, a data analyst can choose from a variety of tools. But as we mentioned, there are some solutions that work better for your unique case. In this post, we will compare two of them: R and SAS. TOC: <ul><li><a href="#define">Defining SAS and R programming</a></li><li><a href="#SAS">Exploring SAS</a></li><li><a href="#compared">Exploring R</a></li><li><a href="#access">Access to New Developments in R vs SAS</a></li><li><a href="#collaboration">Collaboration in R</a></li><li><a href="#cost">Cost of SAS vs R for data science teams</a></li><li><a href="#learn">Should you learn R or SAS?</a></li><li><a href="#functionality">Functionality of SAS compared to R</a></li><li><a href="#hiring">Hiring SAS developers vs R developers</a></li><li><a href="#packages">R programming packages vs SAS tools</a></li><li><a href="#viz">Creating visualizations in R and SAS</a></li><li><a href="#support">Support in SAS vs R</a></li><li><a href="#use">Should you use SAS or R for Clinical Data Science?</a></li><li><a href="#working">Working with SAS vs R</a></li></ul> <hr /> <h2 id="define">What are SAS and R programming?</h2> SAS and R programming are both statistical software used by researchers and data scientists to create statistical data analyses and visualizations. Let’s begin by introducing the tools: <h2 id="SAS">What is SAS?</h2> <a href="https://support.sas.com/software/products/sas-studio/faq/SAS_whatis.htm" target="_blank" rel="noopener">SAS</a> is commercial software that can be used to perform advanced analytics, business intelligence, data management, and predictive analytics. You can use SAS software through both a graphical interface and the SAS programming language. <img class="alignnone size-full wp-image-15364" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01d5c52d671db769e0e42_sas-commercial-software-logo.webp" alt="SAS logo 2022" width="351" height="144" /> <h3>What is SAS programming?</h3> A <a href="https://support.sas.com/software/products/sas-studio/faq/programs_components.htm" target="_blank" rel="noopener">SAS program</a> is a sequence of steps that you submit to SAS for execution. Each step in the program performs a specific task. Only two kinds of steps make up SAS programs: <ol><li style="font-weight: 400;" aria-level="1">DATA steps: in this step data is created, imported, modified, merged, or calculated.</li><li style="font-weight: 400;" aria-level="1">PROC steps: a group of SAS statements that call and execute a procedure, usually with a SAS data set as input. SAS procedures analyze data in SAS data sets to produce statistics, tables, reports, charts, and plots.</li></ol> A SAS program can contain a DATA step, a PROC step, or any combination of DATA steps and PROC steps. The number and kind of steps depend on what tasks you need to perform. <h3>SAS program example</h3> The following example uses SAS to Compare Group Means. The idea is to showcase how the code and output look; not to perform a real analysis. The example data set created consists of only 6 observations. <pre><code> * create example dataset; data patients; input patient_id treatment $ age; cards; 1 a 24 2 a 23 3 a 25 4 b 30 5 b 36 6 b 34 ; run; <br>* compare group means; ods graphics on; <br>proc ttest cochran ci=equal umpu; class treatment; var age; run; <br>ods graphics off; </code></pre> You can see how we created the data in the DATA step and then called the PROC step to perform our analysis. <a href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/statug/statug_ttest_syntax01.htm" target="_blank" rel="noopener">Here</a> you can explore the options used in the PROC step. The output of the analysis looks like this: <img class="alignnone size-full wp-image-15374" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01d5ce4bb5dd953267c1f_SAS-program-output-example-compared-to-R.webp" alt="t-test procedure in sas programming" width="813" height="574" /> <img class="alignnone wp-image-15368" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01d5e5fde21bea0b11479_SAS-distribution-of-age-graph-output-example.webp" alt="" width="500" height="375" /> <img class="alignnone wp-image-15376" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01d5eb556fdf5a5501e83_SAS-Q-Q-plots-of-age-output-example.webp" alt="" width="500" height="285" /> As we can see, SAS provides a lot of information when you run a PROC. You can draw conclusions from both the tables and charts. It’s standard styling and output with no custom branding. <h3>What is the SAS suite?</h3> The SAS software suite is made of components for data management, advanced analytics, multivariate analysis, and more. Here are just a few of some important components of the SAS suite: <ul><li style="font-weight: 400;" aria-level="1"><a href="https://support.sas.com/en/software/base-sas-support.html" target="_blank" rel="noopener">Base SAS</a>: Designed for data access, transformation, and reporting.</li></ul> <ul><li style="font-weight: 400;" aria-level="1"><a href="https://support.sas.com/rnd/app/stat/index.html#s1=6" target="_blank" rel="noopener">SAS/STAT</a>: Designed to perform statistical analysis.</li><li style="font-weight: 400;" aria-level="1"><a href="https://support.sas.com/en/software/sasgraph-support.html" target="_blank" rel="noopener">SAS/GRAPH</a>: Data visualization tool to produce graphs.</li><li style="font-weight: 400;" aria-level="1"><a href="https://support.sas.com/rnd/app/iml/index.html#s1=6" target="_blank" rel="noopener">SAS/IML</a>: Interactive Matrix Language. Includes functions for implementing algorithms.</li></ul> <h2 id="compared">What is R programming compared to SAS?</h2> <a href="https://www.r-project.org/about.html" target="_blank" rel="noopener">R</a> is an open-source language and environment for statistical computing and graphics. It provides a wide variety of statistical techniques such as linear and nonlinear modeling, classical statistical tests, time-series analysis, classification, and clustering. Its most popular IDE (Integrated Development Environment) by far is <a href="https://www.rstudio.com/" target="_blank" rel="noopener">RStudio</a> by RStudio PBC (<a href="https://posit.co/" target="_blank" rel="noopener">Posit</a>). <a href="https://appsilon.com/posit-rstudio-rebrands/" target="_blank" rel="noopener"><img class="alignnone wp-image-15315 size-full" title="RStudio rebrranding Posit" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01d5fb556fdf5a5502138_rebrand-posit-rstudio.webp" alt="rstudio rebrands to posit" width="1724" height="334" /></a> In R, data is stored in objects. These objects can store strings, numeric values, data sets, or anything that can be referenced. To work with these objects we create functions. R has a style of problem-solving centered on functions. <blockquote>Thinking about switching to R and Shiny? See why you might want to <a href="https://appsilon.com/why-you-should-use-r-shiny-for-enterprise-application-development/" target="_blank" rel="noopener">switch to R Shiny for enterprise application development</a>. </blockquote> <h3>R packages</h3> In R, code can be shared through packages. A package is a shareable collection of code that is used to perform a desired function or specific task. Examples of packages used for data science include <a href="https://readr.tidyverse.org/" target="_blank" rel="noopener">readr</a>, <a href="https://dplyr.tidyverse.org/" target="_blank" rel="noopener">dplyr</a>, <a href="https://tidyr.tidyverse.org/" target="_blank" rel="noopener">tidyr</a>, <a href="https://ggplot2.tidyverse.org/" target="_blank" rel="noopener">ggplot2</a>. <img class="alignnone size-full wp-image-15352" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01d607a92f47f6999dda1_open-source-r-packages-available-vs-proprietary-SAS.webp" alt="" width="1025" height="275" /> Anyone can make a package. They can be made publicly available via <a href="https://cran.r-project.org/" target="_blank" rel="noopener">CRAN</a> (Comprehensive R Archive Network), but you can also create private packages that you can use within your organization. As of writing these lines, there are over 18.000 packages available on CRAN! Appsilon contributes to open source as well through our <a href="https://shiny.tools/" target="_blank" rel="noopener">Shiny tools</a>. We create packages to help us, and other R/Shiny developers build scalable, reproducible, and better-looking Shiny applications. <h3>R Program Example</h3> Let’s reproduce the SAS example using R code. Again, the idea is to showcase how code and output look, and not to focus on the interpretation of results. Code: <pre><code> # create example dataset patients <- data.frame( patient_id = 1:6, treatment = rep(c("a", "b"), each = 3), age = c(24, 23, 25, 30, 36, 34) ) <br># compare group means t.test(age ~ treatment, data = patients) </code></pre> Console output: <img class="alignnone size-full wp-image-15378" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01d61ec1e575073de07b4_t-test-R-program-output-example-compared-to-SAS.webp" alt="t-test r programming" width="1149" height="297" /> R output is not styled by default. Nevertheless, there are tools you can use to make the output something you can share in a report, for example, <a href="https://rmarkdown.rstudio.com/" target="_blank" rel="noopener">RMarkdown</a>. The only limitation to output styling is your imagination. You can explore some of our <a href="https://demo.appsilon.com/">Shiny demos</a> from a variety of use cases. <h2>Should you use SAS or R?</h2> In general, you should at least begin incorporating R programming into your data science toolset whether you work in an enterprise or as a private individual. Obviously, the real answer depends on your use case. But as mentioned above, SAS is proprietary, commercial software which can be expensive to the average user. R programming is free with plenty of open source tools and frankly outpaces SAS in a lot of aspects. <blockquote>Curious what R Shiny applications you can build? Explore some of our <a href="https://demo.appsilon.com/" target="_blank" rel="noopener">R Shiny demos</a> to see what you could create.</blockquote> And in this Section, we’ll compare both software on different topics that will affect your choice of SAS or R within an enterprise. <h2 id="access">Access to New Developments in R vs SAS</h2> Open-source software acceptance has increased in recent years. People working together in the community allows for quicker “to market” solutions. It also creates open access to see what lies underneath the hood. There’s no guesswork in how algorithms work or if it’s the best for your case. These new algorithms are developed and shared with the community. Implementation of them in SAS takes longer than in R. This means that <b>more advanced data science techniques might be available right now in R but not yet available in SAS</b>. <h2 id="collaboration">Collaboration in R</h2> File sharing and collaboration are easier with R. If you want to share with a friend or colleague something you developed using SAS, that person requires access to the software - which is licensed. Even though there are some free versions, they require setting up an account which might be something you want to avoid. R is easily downloaded and installed so you can quickly set it up and run code. You can also quickly publish a dashboard on the web using Shiny. <blockquote>Get your data story into the hands of colleagues quickly using these <a href="https://appsilon.com/how-to-share-r-shiny-apps/" target="_blank" rel="noopener">top 3 methods for sharing R Shiny apps</a>. </blockquote> <h2 id="cost">Cost of SAS vs R for data science teams</h2> SAS is <b>commercial </b>software. Meaning, that you must pay to play, so to speak. SAS licenses are known to be expensive so it makes it difficult for individuals and small businesses to use or scale. On the other hand, R is <b>open source</b>. In other words, it’s free to use. Anyone can download it and start using it. <blockquote>Join the Shiny movement and develop your own <a href="https://appsilon.com/r-shiny-dashboard-templates/" target="_blank" rel="noopener">R Shiny dashboard in less than 10 minutes</a>!</blockquote> <h2 id="learn">Should you learn R or SAS?</h2> Whether or not you have experience with programming, we recommend learning R first. It's easy to get started, free, and there are lots of freely accessible learning materials. If you have experience using programming languages, switching to a different language is a matter of learning how to do the things you know, in another place. It usually depends on the resources available for learning. <blockquote>Need a Shiny dashboard now? <a href="https://templates.appsilon.com/" target="_blank" rel="noopener">Download our free Shiny templates and get started today</a>!</blockquote> <h3>R educational resources</h3> R has a lot of free, online resources to get started (e.g., <a href="https://rstudio-education.github.io/hopr/" target="_blank" rel="noopener">Hands-on Programming with R</a> and <a href="https://r4ds.had.co.nz/" target="_blank" rel="noopener">R for Data Science</a>. You can also find books for different topics you want to learn (e.g., <a href="https://bookdown.org/yihui/rmarkdown/" target="_blank" rel="noopener">reporting</a>, and <a href="https://mastering-shiny.org/" target="_blank" rel="noopener">creating web applications</a>). Books are not the only resources available. <a href="https://www.rstudio.com/resources/webinars/" target="_blank" rel="noopener">Join RStudio’s webinars</a>, learn and connect with R users in industry! <h3>SAS educational resources</h3> SAS offers <a href="https://www.sas.com/en_us/training/overview.html" target="_blank" rel="noopener">courses</a> to learn its software. They also have extensive <a href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.3/pgmsaswlcm/home.htm" target="_blank" rel="noopener">documentation</a>. Another thing worth mentioning about SAS is that it offers some products that don’t require knowing how to code (e.g., <a href="https://support.sas.com/en/software/enterprise-guide-support.html" target="_blank" rel="noopener">SAS Enterprise Guide</a>). These tools Access the functionality of SAS from a point-and-click Windows interface. <h2 id=""functionality">Functionality of SAS compared to R</h2> The following table compares how SAS and R work. <table style="height: 373px;" width="511"> <tbody> <tr> <td><b>SAS</b></td> <td><b>R</b></td> </tr> <tr> <td><a href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1kh25to5o0wmvn1o4n4hsl3yyww.htm" target="_blank" rel="noopener">Data steps</a></td> <td>Expressions with functions</td> </tr> <tr> <td><a href="https://support.sas.com/learn/le/proc/index.html" target="_blank" rel="noopener">Procedures</a></td> <td>Expressions with functions</td> </tr> <tr> <td><a href="https://www.tutorialspoint.com/sas/sas_macros.htm" target="_blank" rel="noopener">Macros</a></td> <td>Expressed in R functions</td> </tr> <tr> <td><a href="https://www.tutorialspoint.com/sas/sas_functions.htm" target="_blank" rel="noopener">SAS Functions</a></td> <td><a href="https://www.w3schools.com/r/r_functions.asp" target="_blank" rel="noopener">R functions</a></td> </tr> <tr> <td><a href="https://www.tutorialspoint.com/sas/sas_output_delivery_system.htm" target="_blank" rel="noopener">SAS ODS</a> (Output Delivery System)</td> <td><a href="https://rmarkdown.rstudio.com/" target="_blank" rel="noopener">R Markdown</a>, <a href="https://quarto.org/" target="_blank" rel="noopener">R Quarto</a></td> </tr> </tbody> </table> <h2 id="hiring">Hiring SAS developers vs R developers</h2> Over the last decade, universities have begun to shift from teaching SAS to R. Even domain-specific stats courses tend to use R and train on the RStudio IDE. This means that the R talent pool has increased and will continue to do so in the future. With that being said, R is not as popular as Python for developers. The <a href="http://www.tiobe.com/tiobe-index/" target="_blank" rel="noopener">TIOBE Index for 2022</a> indicates Python is King of the hill at #1 (R is #16, and SAS is a lowly #26). So if stacking your team is a priority and you’re already using Python routines for your analytics, stick with Python. <img class="size-full wp-image-15354" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01d63e37fb5ca1e9145c1_popularity-of-SAS-vs-R-programming-for-skilled-hires.webp" alt="" width="511" height="433" /> Data from <a href="https://pypl.github.io/PYPL.html">PYPL Index</a> If you don’t know already, you can now use <a href="https://www.rstudio.com/blog/three-ways-to-program-in-python-with-rstudio/#:~:text=The%20RStudio%20IDE(opens%20in,would%20in%20an%20R%20script." target="_blank" rel="noopener">Python on RStudio</a>. If you need help setting up your environment with your preferred language on RStudio platforms, contact <a href="https://appsilon.com/?utm_source=template_marketplace&utm_campaign=templates#contact" target="_blank" rel="noopener">Appsilon</a>. We’re RStudio Certified Partners and can help you with <h3>R programming outsourcing and R Shiny consulting</h3> There's a growing number of R-based consultancies popping up as companies begin to expand their data science teams and the need for R Shiny developers to handle more complex data handling and visualization requirements. At Appsilon, we've been creating, maintaining, and developing Shiny applications for enterprise customers all over the world for many years now. Appsilon provides <a href="https://appsilon.com/alternatives-to-scaling-shiny/" target="_blank" rel="noopener">scalability</a>, security, and modern UI/UX with custom R packages that native Shiny apps do not provide. Our team is among the world’s foremost experts in R Shiny and has made a variety of <a href="https://appsilon.com/how-we-built-a-shiny-app-for-700-users/" target="_blank" rel="noopener">Shiny innovations (including scaling to 700 users!)</a> over the years. Appsilon is also a proud <a href="https://appsilon.com/appsilon-data-science-is-now-an-rstudio-full-service-certified-partner/">RStudio (Posit) Full Service Certified Partner</a>. Meaning we can help you throughout the entire process of implementing and scaling RStudio (Posit) products and simplify your data-driven decision-making. Some of the services we, as Shiny consultants provide include: <ul><li>Rapid dashboard development</li><li>Support of full-stack engineers (from setting up a shiny server to UX optimization)</li><li>DevOps support & advisory for all RStudio products</li><li>Machine learning solutions</li><li>Advanced statistical models</li></ul> We deliver world-class Shiny applications faster than other vendors. Ultimately, lowering the overall cost of development and improving time to deployment. We use <b>continuous collaboration</b> with clients, <b>end-to-end testing</b>, and <b>automated processes</b> to streamline the development process. Our team can step in at every phase of a Shiny project, starting from business analysis and data science consulting to code refactoring. <h2 id="packages">R programming packages vs SAS tools</h2> As mentioned, R packages can be developed by anyone. Even though there is no guarantee that they will work as expected, a package that is used by a lot of people is usually something safe to use. The reason is the following: suppose that a package has a bug the creator wasn’t aware of. People start using that package and someone identifies that problem. That person shares that with the creator (and the community) so that it can be fixed. Even someone other than the creator of the package can help code a solution! In R, there is something called the <a href="https://www.tidyverse.org/" target="_blank" rel="noopener">tidyverse</a>, a collection of R packages designed for data science. All packages share an underlying design philosophy, grammar, and data structures. On the other hand, if you detect a problem in SAS you have to communicate with them and wait for a new release with a solution. This might be another reason why things take longer to implement in SAS. <h2 id="viz">Creating visualizations in R and SAS</h2> R provides a variety of packages to create custom charts, both static (<a href="https://ggplot2.tidyverse.org/" target="_blank" rel="noopener">ggplot2</a>) and dynamic (<a href="https://plotly.com/r/getting-started/" target="_blank" rel="noopener">plotly</a>, <a href="https://www.highcharts.com/blog/tutorials/highcharts-for-r-users/" target="_blank" rel="noopener">highcharter</a>). <a href="http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html" target="_blank" rel="noopener">Here</a> you can see lots of different visualizations you can make with R and the code used to produce them! SAS data visualization features are more limited than R and don’t provide as much customization. <h3>SAS Example</h3> In this example, we will create another example dataset. We will create a histogram to understand age distribution by treatment group. SAS’ PROC UNIVARIATE also provides more information which we will not show here because the goal is to see the chart. <pre><code> * create example dataset; data patients; input treatment $ age sex $; cards; a 24 m a 23 m a 25 m a 21 m a 22 f a 22 f a 23 f a 28 f a 21 f a 20 f a 29 f a 18 f a 30 f a 23 f a 25 f a 24 f a 23 f a 25 f b 30 f b 36 f b 34 f b 31 f b 32 m b 32 m b 34 m b 33 m b 34 m b 30 m b 28 m b 33 m b 40 m b 22 m b 29 m ; run; <br>/*create histogram for age variable by treatment*/ proc univariate data=patients; class treatment; var age; histogram age / overlay; run; </code></pre> Output: <img class="alignnone wp-image-15366" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01d65d29d4bcb9685d3af_SAS-data-visualization-example-vs-R.webp" alt="" width="500" height="398" /> <h3>R</h3> We will produce the same chart with R code. <pre><code> # load libraries library(tibble) library(ggplot2) <br># create example dataset patients <- tibble::tribble( ~treatment, ~age, ~sex, "a", 24, "m", "a", 23, "m", "a", 25, "m", "a", 21, "m", "a", 22, "f", "a", 22, "f", "a", 23, "f", "a", 28, "f", "a", 21, "f", "a", 20, "f", "a", 29, "f", "a", 18, "f", "a", 30, "f", "a", 23, "f", "a", 25, "f", "a", 24, "f", "a", 23, "f", "a", 25, "f", "b", 30, "f", "b", 36, "f", "b", 34, "f", "b", 31, "f", "b", 32, "m", "b", 32, "m", "b", 34, "m", "b", 33, "m", "b", 34, "m", "b", 30, "m", "b", 28, "m", "b", 33, "m", "b", 40, "m", "b", 22, "m", "b", 29, "m" ) <br># create chart ggplot(data = patients, aes(x = age, fill = treatment)) + geom_histogram(position = "identity", alpha = 0.5, bins = 9, color = "black") + labs( title = "Distribution of age by treatment", x = "Age (years)", y = "Number of Patients", fill = "Treatment" ) + theme_minimal() + theme( legend.position = "top" ) </code></pre> <img class="alignnone wp-image-15358" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01d65fdb8ad62e9ccb4cd_R-data-visualization-example-vs-SAS.webp" alt="" width="500" height="430" /> We see how with just a few lines of code we are able to create a beautiful chart using R. And we're able to provide more customization to better suit our needs. <h2 id="support">Support in SAS vs R</h2> SAS provides <a href="https://support.sas.com/en/technical-support.html" target="_blank" rel="noopener">Technical Support</a> and has <a href="https://support.sas.com/en/knowledge-base.html" target="_blank" rel="noopener">Documentation</a> with information about everything you can do and how things are implemented in the software. R doesn’t have Technical Support (it is open source) but it has a large community you can reach out to for help. Packages are usually well documented and come with excellent tutorials (called Vignettes) with examples. If you're used to Python, you'll be pleasantly surprised by the quality of documentation that is standard for the R ecosystem (e.g., <a href="https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html" target="_blank" rel="noopener">dplyr vignette</a>, <a href="https://tidyr.tidyverse.org/articles/tidy-data.html" target="_blank" rel="noopener">tidyr vignette</a>). <h2 id="use">Should you use SAS or R for Clinical Data Science?</h2> SAS is great when you need minimal output or sequential processing. But R offers greater flexibility. And with the recent successes by the R Consortium and enhanced collaboration with the FDA, R is trending toward's higher standardization to satisfy regulatory needs. In this section, we will explore how to solve a particular problem using each software. We will use R through <a href="https://www.rstudio.com/" target="_blank" rel="noopener">RStudio IDE</a> and SAS using <a href="https://www.sas.com/en_us/software/on-demand-for-academics.html" target="_blank" rel="noopener">SAS On Demand</a> (which is a free version). <h3 id="statement">Problem Statement</h3> We want to analyze the effect of different variables on mortality due to a particular disease. In particular, we want to understand the differences between treatment application times (no treatment, fast treatment, slow treatment). To do so, we will create a logistic regression model. <h3>Data</h3> The example dataset was created to showcase how to perform the different steps that are usually part of an analysis. It is based on real data, but it has been anonymized and some information was removed from the file (such as disease and treatment names). The focus here is on the <b>how</b> not the <b>what</b>. Data is in .csv format and contains the following information about patients: <ul><li style="font-weight: 400;" aria-level="1"><b>ID</b>: Patient identifier.</li><li style="font-weight: 400;" aria-level="1"><b>AGE</b>: Age of the patient measured in years.</li><li style="font-weight: 400;" aria-level="1"><b>SEX</b>: Sex of the patient (F = Female, M = Male).</li><li style="font-weight: 400;" aria-level="1"><b>CHARLSON</b>: <a href="https://en.wikipedia.org/wiki/Comorbidity#Charlson_index" target="_blank" rel="noopener">Charlson score</a> of the patient.</li><li style="font-weight: 400;" aria-level="1"><b>PITT</b>: <a href="https://www.ncbi.nlm.nih.gov/pmc/articles/PMC7156778/#:~:text=The%20Pitt%20bacteremia%20score%20(PBS)%20is%20widely%20used%20in%20infectious,risk%20of%20death%20%5B9%5D." target="_blank" rel="noopener">Pitt bacteremia score</a> of the patient.</li><li style="font-weight: 400;" aria-level="1"><b>SURVIVED</b>: Indicator variable. 1 means the patient was cured.</li><li style="font-weight: 400;" aria-level="1"><b>DIED_OF_DISEASE</b>: Indicator variable. 1 means the patient died due to the disease.</li><li style="font-weight: 400;" aria-level="1"><b>DIED_OTHER</b>: Indicator variable. 1 means the patient died due to another cause.</li><li style="font-weight: 400;" aria-level="1"><b>UNKNOWN</b>: Indicator variable. 1 means status was lost for the patient.</li><li style="font-weight: 400;" aria-level="1"><b>TREATMENT</b>: Indicator variable. 1 means the patient received treatment.</li><li style="font-weight: 400;" aria-level="1"><b>TREATMENT_FAST</b>: Indicator variable. 1 means treatment was applied within 48hs.</li></ul> In the following Sections we will see how to perform different tasks with SAS and R: <ul><li style="font-weight: 400;" aria-level="1">Read data</li><li style="font-weight: 400;" aria-level="1">Wrangle data (apply filters, add and remove columns)</li><li style="font-weight: 400;" aria-level="1">Create a logistic regression model</li><li style="font-weight: 400;" aria-level="1">Explore outputs</li></ul> <h2 id="working">Working with SAS vs R</h2> <h3>Reading data in SAS vs R programming</h3> To start working with data, first, we need to have access to it. <h4>SAS:</h4> <pre><code> * READ DATA; FILENAME REFFILE '/home/u4729884/data.csv'; PROC IMPORT DATAFILE=REFFILE DBMS=CSV OUT=WORK.RAW_DATA; GETNAMES=YES; RUN; </code></pre> <h4>R programming:</h4> <pre><code> # Read data raw_data <- read.csv("data.csv") </code></pre> <h3>Data Wrangling in SAS vs R programming</h3> Now that we have data available, we will prepare it for the model. In this Section we will show how to: <ul><li style="font-weight: 400;" aria-level="1">Remove rows corresponding to patients with unknown status</li><li style="font-weight: 400;" aria-level="1">Remove rows corresponding to patients that died due to another cause</li><li style="font-weight: 400;" aria-level="1">Create a new treatment variable using values found in other columns</li><li style="font-weight: 400;" aria-level="1">Create new indicator variables for age, Charlson and Pitt using specific cutpoints</li><li style="font-weight: 400;" aria-level="1">Select columns</li></ul> This is not the only way one could process the data. We choose to do it this way for simplicity. Feel free to try something different and share your results! <h4>SAS:</h4> <pre><code> * DATA WRANGLING; DATA WORK.DATA_TO_MODEL (KEEP = AGE_60 SEX CHARLSON_4 PITT_3 TREATMENT_CATEGORY DIED_OF_DISEASE); * KEEP VARS OF INTEREST; SET WORK.RAW_DATA; * REMOVE PATIENTS WITH UNKNOWN STATUS; IF UNKNOWN = 1 THEN DELETE; * REMOVE PATIENTS THAT DIED DUE TO OTHER CAUSE; IF DIED_OTHER_CAUSE = 1 THEN DELETE; * CREATE TREATMENT FACTOR VARIABLE; LENGTH TREATMENT_CATEGORY $14; IF TREATMENT = 0 THEN TREATMENT_CATEGORY = "NO TREATMENT"; ELSE IF TREATMENT_FAST = 1 THEN TREATMENT_CATEGORY = "FAST TREATMENT"; ELSE TREATMENT_CATEGORY = "SLOW TREATMENT"; * NEW AGE VARIABLE; IF AGE >= 60 THEN AGE_60 = 1; ELSE AGE_60 = 0; * NEW CHARLSON VARIABLE; IF CHARLSON > 4 THEN CHARLSON_4 = 1; ELSE CHARLSON_4 = 0; * NEW PITT VARIABLE; IF PITT > 3 THEN PITT_3 = 1; ELSE PITT_3 = 0; RUN; </code></pre> <h4>R programming:</h4> <pre><code> # Load required library library(dplyr) # Data wrangling data_to_model <- raw_data |> # Filter rows filter( UNKNOWN != 1, DIED_OTHER_CAUSE != 1 ) |> # Create new columns mutate( TREATMENT_CATEGORY = case_when( TREATMENT == 0 ~ "NO TREATMENT", TREATMENT_FAST == 1 ~ "FAST TREATMENT", TRUE ~ "SLOW TREATMENT" ), AGE_60 = ifelse(AGE >= 60, 1, 0), CHARLSON_4 = ifelse(CHARLSON > 4, 1, 0), PITT_3 = ifelse(PITT > 3, 1, 0) ) |> # Select columns select( AGE_60, CHARLSON_4, PITT_3, TREATMENT_CATEGORY, DIED_OF_DISEASE ) </code></pre> <h3>Modeling data in SAS vs R programming</h3> Once data is processed, we are ready to model. We will create a logistic regression model where we will model the probability of dying due to the disease. As explanatory variables we will include: <ul><li style="font-weight: 400;" aria-level="1">Dichotomized age (cutpoint: 60 years, reference: age > 60)</li><li style="font-weight: 400;" aria-level="1">Dichotomized Charlson score (cutpoint: score of 4, reference: score > 4)</li><li style="font-weight: 400;" aria-level="1">Dichotomized Pitt score (cutpoint: score of 3, reference: score > 3) </li><li style="font-weight: 400;" aria-level="1">Treatment (Factor with three levels: no treatment, fast treatment, slow treatment). We will use fast treatment as a reference.</li></ul> <h4>SAS:</h4> <pre><code> * MODELING; PROC LOGISTIC DATA = WORK.DATA_TO_MODEL DESCENDING; CLASS TREATMENT_CATEGORY (REF = "FAST TREATMENT") SEX (REF = "F") / PARAM = REFERENCE; MODEL DIED_OF_DISEASE = AGE_60 CHARLSON_4 PITT_3 TREATMENT_CATEGORY / LINK = LOGIT SCALE = NONE; RUN; </code></pre> <h4>R programming:</h4> <pre><code> # Create model model <- glm(formula = DIED_OF_DISEASE ~ ., data = data_to_model, family = binomial) <br># Explore results summary(model) <br># Get odds ratio exp(cbind(coef(model), confint(model, level = 0.95))) </code></pre> <h3>Exploring model results in SAS vs R programming</h3> <h4>SAS:</h4> <img class="alignnone size-full wp-image-15372" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01d679506383404c3caaf_SAS-model-results-example.webp" alt="" width="958" height="636" /> <img class="alignnone size-full wp-image-15370" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01d68afc0ff7c6578b0cd_SAS-model-results-example-2.webp" alt="" width="936" height="724" /> Let's focus on these two tables: <img class="alignnone size-full wp-image-15362" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01d6a6cd8f7584c80b363_SAS-coefficients-of-model-output.webp" alt="" width="819" height="525" /> Here we can see the coefficients of the model, its significance level, and the translation to odds ratio estimates (which are more interpretable when doing logistic regression. We will not dive into details for explaining model assessment or results interpretation. The idea here is to show where this information is available and how to get it using code. <h4>R programming:</h4> <img class="alignnone size-full wp-image-15360" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01d6b4cedf11e5f973ede_R-programming-model-results.webp" alt="" width="937" height="649" /> In R, we have to compute the odds ratio using the model coefficients. It can be done with: <img class="alignnone size-full wp-image-15356" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01d6c3c93260f45209538_R-compute-odds-ratio.webp" alt="" width="933" height="217" /> <h2 id="final">Final comparison of SAS vs R programming</h2> General remarks: <ul><li style="font-weight: 400;" aria-level="1">With both SAS and R we arrived at the same results.</li><li style="font-weight: 400;" aria-level="1">SAS code requires the use of a semicolon ( ; ) to end statements as well as an explicit RUN to run code. This makes SAS more prone to distraction errors.</li><li style="font-weight: 400;" aria-level="1">By default, SAS shows more information in its output and has some styling.</li></ul> Detailed comparison: <ul><li style="font-weight: 400;" aria-level="1">Reading data. Importing data with SAS involves more lines of code, with somewhat cryptic parameters. In R, it was straightforwardly done with a function available by default when installing R.</li><li style="font-weight: 400;" aria-level="1">Data wrangling. SAS syntax seems more complicated than R. In R, thanks to the use of the <a href="https://stat.ethz.ch/R-manual/R-devel/library/base/html/pipeOp.html" target="_blank" rel="noopener">pipe operator</a>, code is easier to read. Also, we can follow what is going on in each step. In SAS, some parts are defined at the beginning (for example, the columns to keep). Also, when creating the new treatment variable, in SAS we had to define its type before being able to create it, which might seem counter-intuitive.</li><li style="font-weight: 400;" aria-level="1">Modeling. Again, SAS code seems more verbose than R in order to accomplish this task. One thing to mention here is that in both software there are multiple ways to get to the same result, so maybe another person can write code in a more succinct way.</li><li style="font-weight: 400;" aria-level="1">Exploring results. Even though SAS output looks nicer, maybe you want to write a report using part of those results. Right now, it would imply copying and pasting tables in a document and sharing it. Regarding R, we have shown what the output looks like in the console. With R you can create reports with code in the same file using <a href="https://rmarkdown.rstudio.com/" target="_blank" rel="noopener">R Markdown</a>. One more thing to mention is that SAS by default computed the odds ratio. If you want to access those values in R you need to apply computations using the model results.</li></ul> <h2>Should You Choose SAS or R? (Conclusion)</h2> If you're looking to keep pace within your industry or create faster tooling and PoCs for your team, you should consider switching to R programming. SAS still holds value for a lot of users, but R and its open source packages are becoming the standard for the new workforce. Don't get left behind! In this post, we introduced SAS and R, talked about some differences between them, and showcased an example written in both languages. If you're interested in getting started with R or Shiny for your data science needs, let's talk. The following list summarizes the advantages of R over SAS: <ul><li style="font-weight: 400;" aria-level="1">R is free to use. SAS is expensive.</li><li style="font-weight: 400;" aria-level="1">R code is more easily shared.</li><li style="font-weight: 400;" aria-level="1">State-of-the-art implementations are usually available in R before SAS.</li><li style="font-weight: 400;" aria-level="1">More and more people are being taught R.</li><li style="font-weight: 400;" aria-level="1">R code has fewer syntax rules.</li><li style="font-weight: 400;" aria-level="1">R charts are more customizable than SAS charts</li></ul>