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.
In this post, we'll demonstrate how seamless this integration can be by walking through a practical scenario:
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.
Begin by starting a workbench by selecting the Launch VS Code - SAS and R programming interface.
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...
The following code pre-processes the data for analysis by doing the following:
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.
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.
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)
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.
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")
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')
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.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.