BookmarkSubscribeRSS Feed

Introducing PROC R: The Newest Way to Integrate R and SAS (Part 1)

Started ‎04-02-2026 by
Modified ‎04-02-2026 by
Views 575

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!

 

About PROC R

 

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;
 
SL_1_helloR-1536x643.png

 

 

Pass Data Between SAS and R

 
Users can easily pass data sets from SAS to R and back using the following functions:
 
sd2df or sasdata2dataframe: exports a SAS data set to a local R data frame using the haven package.
 
df <- sd2df("sashelp.cars") # Exports sashelp.cars to an R data frame
 
df2sd of dataframe2sasdata: transfers an R data frame to a SAS data set.
 
df2sd(df, "cars_r", "work") # Converts the df data frame into a data set in work.cars_r
 
The following example converts SASHELP.CARS to an R data frame and uses the filter() function from the tidyverse collection of packages in R to only keep Audi vehicles. Then, the filtered data frame is converted to a SAS table called AUDI stored in the WORK library. Finally, we print the newly created table with a PROC PRINT step.
 
 
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;

 

SL_2_conversionprocr-1536x722.png


Retrieve and Set Macro Variables

 

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;


SL_3_Honda-1024x366.png

 

Output Results

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

 

Without show()

 

proc r;
    submit;
    head(carsAudi);
    endsubmit;
run;

 

SL_4_logresults-1024x513.png

 

With show()

 

proc r;
    submit;
    show(head(carsAudi), title = "Audi Vehicles");
    endsubmit;
run;

 

SL_5_showfunc-1024x323.png

 
Create and View R Plots

 

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;

SL_6_rplot-1024x495.png

 

Conclusion

 

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.

Contributors
Version history
Last update:
‎04-02-2026 11:02 AM
Updated by:

Catch up on SAS Innovate 2026

Dive into keynotes, announcements and breakthroughs on demand.

Explore Now →

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 Labels
Article Tags