BookmarkSubscribeRSS Feed

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

Started 7 hours ago by
Modified 7 hours ago by
Views 71

Last month I started a series introducing PROC R, the newest way to integrate R and SAS. In that post, I covered several key functions:

 

  • sd2df() and df2sd() for moving data between SAS and R
  • symget() and symput() for retrieving and setting macro variables
  • show() for displaying R output in the SAS Results window instead of the log

 

In this month’s post, we’ll build on that foundation by introducing additional functions for calling SAS routines from R and creating and displaying plots. Let’s get started!

 

 

Call a SAS Function

 

The sasfnc() function allows users to call SAS functions within the PROC R procedure. It works by constructing a %SYSFUNC macro call and retrieves the results.

 

For example, the following code uses the SAS function propcase() to capitalize the values in the CITY column of the SASHELP.HOMEEQUITY table and it saves them in a new column called CITY2:

 

proc R;
submit;

df <- sd2df("sashelp.homeequity")

df$CITY2 <- sapply(df$CITY, function(x) sasfnc("propcase", x))

show(head(df[, c("CITY", "CITY2")]))

endsubmit;
run;

 

ST_01_cityoutput.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.

 

 

Display R Plots

 

The rplot() function enables users to display R graphics in the SAS results pane. This is a major improvement over PROC IML which only allows users to save plots as static images.

 

Users can create plots with base R, or take advantage of the many graphic options available in the ggplot2 package.  Note that PROC R allows you to display more than one plot at a time within a single PROC R step.

 

 

Base R Plot

proc r;
submit;

set.seed(123)
x <- seq(10, 200, length.out = 120)
y <- 0.5 * x + 20 + rnorm(120, 0, 30)

rplot({
  plot(x, y,
       main = "Base R Plot",
       xlab = "Input Variable",
       ylab = "Response Variable",
       col = "darkgreen",
       pch = 17,         
       cex = 1.2) 
  
  abline(lm(y ~ x), col = "purple", lwd = 3, lty = 2)

})

endsubmit;
run;

 

ST_02_baseRplot.png

 

 

Ggplot2 Plot

proc r;
submit;
library(ggplot2)

df <- data.frame(
  value = rnorm(200, mean = 50, sd = 10)
)

p <- ggplot(df, aes(x = value)) +
  geom_histogram(aes(y = ..density..),
                 bins = 20,
                 fill = "skyblue",
                 color = "white") +
  geom_density(color = "red", linewidth = 1) +
  theme_minimal() +
  labs(title = "Ggplot2 Plot",
       x = "Value",
       y = "Density")

rplot(p)

endsubmit;
run;

 

03_ST_ggplot2-plot.png

 

 

Multiple Plots at Once

proc r;
submit;
library(ggplot2)
library(dplyr)

shoes_df <- sd2df("sashelp.shoes")

shoes_df$Region <- as.factor(shoes_df$Region)
shoes_df$Product <- as.factor(shoes_df$Product)

# -------------------------------------------------
# Plot 1: Sales by Region
# -------------------------------------------------
region_sales <- shoes_df %>%
  group_by(Region) %>%
  summarise(total_sales = sum(Sales, na.rm = TRUE))

p1 <- ggplot(region_sales, aes(x = reorder(Region, total_sales), y = total_sales)) +
  geom_col(fill = "steelblue", alpha = 0.85) +
  coord_flip() +
  theme_minimal() +
  labs(title = "Total Shoe Sales by Region",
       x = "Region",
       y = "Total Sales")

rplot(p1)


# -------------------------------------------------
# Plot 2: Sales vs Inventory
# -------------------------------------------------
p2 <- ggplot(shoes_df, aes(x = Inventory, y = Sales)) +
  geom_point(aes(color = Region), alpha = 0.6, size = 2) +
  geom_smooth(method = "lm", se = TRUE, color = "black") +
  theme_minimal() +
  labs(title = "Sales vs Inventory Levels",
       x = "Inventory",
       y = "Sales")

rplot(p2)


# -------------------------------------------------
# Plot 3: Sales Distribution Across Regions and Product Categories
# -------------------------------------------------
shoes_df <- sd2df("sashelp.shoes")

heat_df <- shoes_df %>%
  group_by(Region, Product) %>%
  summarise(total_sales = sum(Sales, na.rm = TRUE))

p3 <- ggplot(heat_df, aes(x = Product, y = Region, fill = total_sales)) +
  geom_tile() +
  scale_fill_gradient(low = "white", high = "steelblue") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(title = "Sales Heatmap: Region vs Product",
       x = "Product",
       y = "Region")

rplot(p3)

endsubmit;
run;

 

multiplots.gif

 

 

Render an Image File

 

Users can also render an image file to the Results pane by using the renderImage() function:

 

proc r;
submit;
    library(ggplot2)
    
    df <- data.frame( value = rnorm(200, mean = 50, sd = 10) )
    p <- ggplot(df, aes(x = value)) +
    geom_histogram(aes(y = ..density..),
    bins = 20, 
    fill = "skyblue", 
    color = "white") + 
    geom_density(color = "red", linewidth = 1) + 
    theme_minimal() + 
    labs(title = "Ggplot2 Plot", x = "Value", y = "Density")
    
    # saves to sas@workpath automatically
    rplot(p, filename = "myplot.png")
    
    # To render it again, must provide full path
    full_path <- paste0("/tmp/Rtmp94GW2f//", "myplot.png")
    renderImage(full_path)
endsubmit;
run;

 

 

Conclusion

 

In this post, we built on the basics of PROC R and explored how it deepens the integration between SAS and R. We saw how sasfnc() lets you call SAS functions directly from R, and how rplot() brings R visualizations straight into the SAS Results window.

We also covered generating multiple plots in a single PROC R step and rendering image files directly into SAS output. Together, these features make it easy to move between SAS and R without breaking your workflow.

 

 

Helpful Links

 

Part 1 of series

 

SAS PROC R Deep Dive

 

PROC R Documentation

 

 

Find more articles from SAS Global Enablement and Learning here.

Contributors
Version history
Last update:
7 hours ago
Updated by:

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

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 Tags