BookmarkSubscribeRSS Feed

The Best of Both Worlds: Using SAS and R Together in SAS Viya Workbench

Started ‎07-25-2025 by
Modified ‎07-29-2025 by
Views 696

Why choose between SAS and R when you can use both — together, in one environment? SAS Viya Workbench makes it easy to jump between languages without switching tools, copying files, or breaking your train of thought.

 

With a shared file system and support for notebooks across multiple languages, SAS Viya Workbench lets you mix and match SAS and R in the same project. It's a flexible, modern setup that keeps everything connected and in sync — so you can stay focused on the data, not the logistics.

 

 

What This Post Will Cover

 

In this post, we'll demonstrate how seamless this integration can be by walking through a practical scenario:

 

  1. Use SAS to prepare and filter data
  2. Save the data to a SAS library
  3. Load that data in an R notebook for analysis and visualization.

 

All within SAS Viya Workbench!

 

To keep things concrete, we’ll use the built-in SASHELP.CARS dataset. This dataset contains specifications and performance data for over 400 vehicle models, including variables such as make, model, type, origin, drivetrain, horsepower, and fuel efficiency.

 

 

Step 1: Prepare the Data Using a SAS Notebook

 

Begin by starting a workbench by selecting the Launch VS Code - SAS and R programming interface.

 

01_ST_vs-code.png

Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.

 

Open a SAS notebook by selecting the main menu > File > New File...


02_ST_filenew.png

 

03_ST_sasnotebook.png

 

The following code pre-processes the data for analysis by doing the following:

 

  • Defines the SAS library MYLIB connected to our data folder mydata.
  • Filters the SASHELP.CARS table to include only SUVs with an MSRP less than $40,000.
  • Keeps only relevant columns.
  • Saves the result as a table called FILTERED_CARS in the MYLIB library.

 

libname mylib "/workspaces/myfolder/mydata";
data mylib.filtered_cars;
    set sashelp.cars;
    where Type = "SUV" and MSRP < 40000;
    keep Make Model Type Origin DriveTrain MSRP MPG_City Horsepower;
run;

 

Now filtered_cars.sas7bdat will appear in the mydata folder, ready to be used in an R notebook.

 

04_ST_saveddata-1.png

 

 

Step 2: Load the Data in R

 

Next, create a Jupyter Notebook by selecting the main menu > File > New File > Jupyter Notebook. You could also create an R document or R markdown if you prefer. Jupyter notebooks are nice for running code and seeing output directly below the code cell. The default kernel selected is Python but can be changed to R by clicking the bottom right of the cell where it says "Python" and choosing R instead.

 

05_ST_jupyter.png

 

06_ST_kernel-1024x289.png

Because all notebooks in Workbench share the same file system, you can load the dataset directly with the read_sas() function from the haven package and provide the file path to the filtered cars data:

 

library(haven)

cars_df <- read_sas("/home/workspace/shared_data/filtered_cars.sas7bdat")

# Preview
head(cars_df)

 

07_ST_preview1.png

 

Note, if you do not have the haven package, or any other R package you need installed, you can install the package directly by running install.packages("package_name") in a code cell.

 

 

Step 3: Create a Summary Table with Kable

 

Next, we create a summary table using the dplyr and knitr packages in R. This summary table gives the average of MPG_City, Horsepower, and MSRP for each level of DriveTrain.

 

library(knitr)
library(dplyr)

summary_table <- cars_df %>%
  group_by(DriveTrain) %>%
  summarise(
    Count = n(),
    Avg_MPG_City = round(mean(MPG_City, na.rm = TRUE), 1),
    Avg_Horsepower = round(mean(Horsepower, na.rm = TRUE), 1),
    Avg_MSRP = round(mean(MSRP, na.rm = TRUE), 0)
  )

# Display table
kable(summary_table, caption = "Summary by Drivetrain")

 

08_ST_Drivetrain.png

 

 

Step 4: Visualize with Plotly

 

To quickly explore the data, create an interactive scatter plot using the R package plotly, showing city MPG vs. horsepower, colored by drivetrain. plotly allows for users to interact with their plots. For example, users can hover over points to see more detailed information or zoom into a specific area of the plot for a closer look at specific data points.

 

library(plotly)

library(dplyr)
library(plotly)
# Plot it
plot_ly(
  data = cars_df,
  x = ~Horsepower,
  y = ~MPG_City,
  color = ~DriveTrain,
  type = 'scatter',
  mode = 'markers',
  marker = list(size = 15),
  text = ~paste("Make:", Make,
                "
Model:", Model,
                "
MPG:", MPG_City,
                "
HP:", Horsepower)
) %>% layout(title = 'City MPG and Horsepower')

 

09_ST_plotly1.gif

 

 

Conclusion

 

SAS Viya Workbench makes cross-language workflows seamless. By supporting both SAS and R in a shared environment, it allows users to prepare, explore, and analyze data without switching platforms. This integration streamlines collaboration and simplifies complex workflows, helping you stay focused on solving problems — not managing tools.

 

For general information about SAS Viya Workbench visit SAS Viya Workbench | SAS

 

Learn more about using R in SAS Viya Workbench here: Getting Started with R in SAS Viya Workbench

 

For step-by-step instructions to start a workbench instance visit SAS Tutorial | Getting Started with SAS Viya Workbench for Learners.

 

Students and educators can access SAS Viya Workbench for Learners for free: SAS Viya Workbench for Learners | SAS

 

 

Find more articles from SAS Global Enablement and Learning here.

Version history
Last update:
‎07-29-2025 02:23 PM
Updated by:

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

SAS AI and Machine Learning Courses

The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.

Get started

Article Tags