In this series, we have explored several ways to incorporate R code into SAS programs and create R scripts within SAS using PROC R. In this post, we'll take that integration a step further by using the R Program step in SAS Data and AI Studio flows. Flows provide a visual way to combine SAS data preparation and processing with R programming, allowing you to take advantage of the strengths of both languages within a single workflow.
Note: This post is based on the 2026.06 stable release of SAS Viya.
If you are asking yourself, “What is a ‘flow’?”, SAS Data and AI Studio Flows offer a low-code option for analyzing data with SAS Viya. A flow is a sequence of operations on data. Data and operations are represented in SAS Data and AI Studio by steps that you can access from the Steps section of the navigation pane. Each step in a flow is represented by a node on the flow canvas. While SAS provides several predefined steps to use in flows, users and developers also build custom steps to perform specific tasks. The R Program step is a predefined step provided by SAS; however, users can also use R code to create their own custom steps.
exampleflow1.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.
Additional resources regarding SAS Studio Flows:
To add the R Program step to a flow, click the Steps icon in the lefthand pane. From the SAS Steps tab, expand the Develop folder and either click and drag the R Program step to the flow or right-click the step and select Add to flow.
rprogramstep.gif
Users can write R code directly in the R Program step’s code editor without connecting it to other nodes in the flow. The example below uses the same R code from the previous post in this series, Introducing PROC R (Part 3): Creating and Calling R Scripts. It analyzes the SASHELP.CARS data set by creating a histogram of highway miles per gallon and a table of Honda vehicle information. To run the example, copy and paste the code into the R Program code editor and run the flow.
library(tidyverse)
VMake <- "Honda"
carsdf <- sd2df("sashelp.cars")
mycars <- carsdf %>%
filter(Make == VMake)
# Create a macro variable in SAS from R
avg_msrp <- round(mean(mycars$MSRP, na.rm = TRUE))
df2sd(mycars, "work.filtered_cars")
p <- ggplot(mycars, aes(x = MPG_Highway)) +
geom_histogram(binwidth = 5, 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)
show(head(mycars), paste0("First 5 ", VMake," Cars (Avg MSRP = ", "$", format(avg_msrp, big.mark = ","), ")"))
inlinecode3-1024x488.png
inlinecoderesults.gif
From the Node tab, users can rename the step, create an external reference to the code by saving it or selecting an existing R script, and provide a description of the node.
nodespecs2-1024x495.png
This example shows how to use the R Program step as part of a larger flow. SAS steps first prepare the data, and then the resulting table is passed to R for further analysis and visualization. By combining SAS and R in one flow, you can use each language where it works best—such as incorporating an existing R workflow or package into a broader SAS process without moving the entire analysis outside of SAS. In this flow, SAS prepares the HOMEEQUITY data by removing rows with missing loan or property values and calculating a loan-to-value (LTV) ratio. R then categorizes loans by LTV, creates summary statistics and a visualization with the dplyr and ggplot2 packages, and saves the enhanced data as a new SAS table.
homeequityflow4.png
Create the flow by following these steps:
step1-1.gif
step2.gif
Untitled-Project.gif
Untitled-Project2.gif
step-4.gif
library(dplyr)
library(ggplot2)
homeequity <- sd2df("HOMEEQUITY_SAS")
# Create an LTV category
homeequity <- homeequity %>%
mutate(
LTV_Category = case_when(
LTV < 0.5 ~ "Low",
LTV < 0.8 ~ "Medium",
TRUE ~ "High"
)
)
# Create a summary table by LTV category
loan_summary <- homeequity %>%
group_by(LTV_Category) %>%
summarise(
Number_of_Loans = n(),
Average_Loan = mean(LOAN, na.rm = TRUE),
Average_Value = mean(VALUE, na.rm = TRUE),
Average_LTV = mean(LTV, na.rm = TRUE),
.groups = "drop"
)
# Create a scatter plot showing loan amount
# versus property value by LTV category
ltv_plot <- ggplot(
homeequity,
aes(
x = VALUE,
y = LOAN,
color = LTV_Category
)
) +
geom_point(
alpha = 0.65,
size = 2
) +
geom_smooth(
method = "lm",
se = FALSE,
linewidth = 1.1
) +
scale_color_manual(
values = c(
"Low" = "#4C78A8",
"Medium" = "#F2A541",
"High" = "#D95F59"
)
) +
scale_x_continuous(
labels = scales::label_dollar()
) +
scale_y_continuous(
labels = scales::label_dollar()
) +
labs(
title = "Home Equity Loans by Loan-to-Value Category",
subtitle = "Relationship between property value and loan amount",
x = "Property Value",
y = "Loan Amount",
color = "LTV Category"
) +
theme_minimal(base_size = 12) +
theme(
plot.title = element_text(
face = "bold",
size = 16
),
plot.subtitle = element_text(
size = 11,
margin = margin(b = 15)
),
axis.title = element_text(
face = "bold"
),
legend.title = element_text(
face = "bold"
),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank(),
plot.title.position = "plot"
)
# Convert homeequity table from R data frame to SAS table
df2sd(homeequity, "HOMEEQUITY_LTV")
# Display the outputs
show(loan_summary)
rplot(ltv_plot)
step-5.gif
resultsR.gif
The R Program step extends SAS Data and AI Studio flows by making it easier to bring R into a visual SAS workflow. In this example, SAS steps prepared the data, R performed additional analysis and visualization, and the results were returned to SAS for continued use. This approach lets you use R where it adds value while keeping the overall process organized and reusable within SAS.
This is the fourth post in a series of posts introducing PROC R. If interested, parts 1, 2, and 3 can be found here:
Part 1: Introducing PROC R (Part 1): The Newest Way to Integrate R and SAS
Part 2: Introducing PROC R (Part 2): Creating R Plots Within SAS Programs
Part 3: Introducing PROC R (Part 3): Creating and Calling R Scripts
Note: To access PROC R, users should have access to SAS Viya 2026.03 or later.
Find more articles from SAS Global Enablement and Learning here.
Visit the Tips & Tricks page for setup guidance, demos, and practical examples that show how Copilot supports your workflows.
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.