When discussing programming languages, people often position R and SAS as competitors. If you primarily program in R and are new to SAS—or vice versa—you might be surprised by how many ways you can combine the strengths of both languages. The newest and arguably most seamless way to integrate R and SAS is with the R procedure (PROC R). It lets you run R code directly inside SAS, while keeping your data in the SAS ecosystem. This post will introduce PROC R, highlight some of the key functions that enable communication between SAS and R, and walk through a few simple examples to get you started running R code directly inside SAS. Let’s get started!
PROC R is available to SAS Viya users starting with the 2026.03 SAS Viya release. The syntax and functionality is similar to PROC Python, and many of the functions available may seem familiar if you have any experience with PROC Python. For more information on PROC Python visit SAS Help Center: Overview: PROC PYTHON
You can include a PROC R step directly within a SAS program. The step starts with a PROC R statement, ends with a RUN statement, and any R code is enclosed within SUBMIT and ENDSUBMIT statements. This example prints a simple message that appears in the log.
proc r;
submit;
print('Hello from R!');
endsubmit;
run;
proc r;
submit;
library(tidyverse)
carsdf <- sd2df("sashelp.cars")
carsAudi <- carsdf %>% filter(Make == "Audi")
df2sd(carsAudi, "AUDI", "WORK");
endsubmit;
run;
proc print data=work.audi;
run;
Users can retrieve and set macro variables within PROC R with the symget() and symput() functions.
symget(): retrieves the value of a SAS macro variable.
symget("Make") # Retrieves the value of the Make macro variable
symput(): sets the value of a SAS macro variable.
symput("Make", "Audi") # Sets the value of Make to Audi
This example shows how symget()and symput()enable two-way communication between SAS and R inside PROC R. The SAS macro variable Make is passed into R using symget(), where it is used to filter the SASHELP.CARS data set and calculate the average of city and highway MPG. That result is then sent back to SAS using symput(), creating a new macro variable (AvgMPG) that is used directly in the SAS report title.
%let Make = Honda;
/* R: filter data and create a macro variable */
proc r;
submit;
library(tidyverse)
VMake <- symget("Make")
carsdf <- sd2df("sashelp.cars")
mycars <- carsdf %>%
filter(Make == VMake)
/* Create a macro variable in SAS from R */
avg_msrp <- mean(mycars$MSRP, na.rm = TRUE)
symput("AvgPrice", avg_msrp)
df2sd(mycars, "filtered_cars", "WORK")
endsubmit;
run;
/* SAS: use the macro variable created in R */
%put Average MSRP from R = &AvgPrice;
proc print data=filtered_cars(obs=5);
title "First 5 &Make Cars (Avg MSRP = &AvgPrice)";
run;
The show() function sends output created in the PROC R step to the Results window instead of the log.
show(carsdf,title="Cars Data" ,count=5) # Prints the first 5 rows of the carsdf data frame with Cars Data as the title
proc r;
submit;
head(carsAudi);
endsubmit;
run;
proc r;
submit;
show(head(carsAudi), title = "Audi Vehicles");
endsubmit;
run;
PROC R allows you to take advantage of R graphing capabilities, such as the wealth of options available within the ggplot2 package. After creating and storing a plot, the rplot() function outputs the graphic to the Results window in SAS. This code creates a polished histogram of the MPG_HIGHWAY variable from the carsdf data set created earlier, showing the distribution of highway miles per gallon across vehicles. It uses custom colors, a minimal theme, and bold, centered titles to make the plot clear and visually appealing.
proc r;
submit;
library(ggplot2)
p <- ggplot(carsdf, aes(x = MPG_Highway)) +
geom_histogram(binwidth = 2, fill = "#69b3a2",
color = "#1f3552", alpha = 0.8) +
labs(
title = "Distribution of Highway MPG",
x = "Highway MPG",
y = "Count"
) +
theme_minimal(base_size = 14) +
theme(
plot.title = element_text(hjust = 0.5, face = "bold"),
axis.title = element_text(face = "bold"),
panel.grid.minor = element_blank()
)
rplot(p);
endsubmit;
run;
PROC R makes it easier than ever to bring the strengths of R into your existing SAS workflows without needing to move data or switch environments. Whether you’re using R for data wrangling, visualization, or specialized analysis, PROC R allows you to seamlessly integrate those capabilities while still leveraging SAS for data access, preparation, and downstream reporting.
This post introduced the basics of how to run R code, pass data between SAS and R, and create output and visualizations; however, it’s just the beginning. In future posts, we’ll explore more functions and use cases that highlight when and why combining SAS and R can be especially powerful.
If there are specific examples or use cases you’d like to see, feel free to reach out! I’d love to hear how you’re thinking about using PROC R.
Dive into keynotes, announcements and breakthroughs on demand.
Explore Now →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.