Technology Tales

Notes drawn from experiences in consumer and enterprise technology

Running R code within SAS programs with PROC R

Published on 28th November 2026 Estimated Reading Time: 9 minutes

A presentation on the new PROC R procedure at PHUSE EU Connect 2026 in Glasgow covered how R and SAS can be used together rather than as alternatives. SAS can manage data, prepare it for analysis and feed reports, while R offers a wide range of packages for statistics and visualisation. PROC R, which arrived with the SAS Viya 2026.03 release, lets the two meet inside one SAS program or flow.

In practice, this means nothing needs to move between separate working environments. Code written in R is submitted from SAS, and datasets and macro variables pass in either direction. Output and plots from R then appear in the SAS results, and the sections below begin with how PROC R sits among the other ways of linking the two languages.

How PROC R Relates to Other Ways of Combining SAS and R

SAS has offered ways to reach R for a long time, and PROC R is the latest of them. Calling R from PROC IML has been possible since SAS/IML 9.22. The second part of a SAS Communities series on PROC R notes that PROC IML only lets users save plots as static images, whereas PROC R displays R graphics in the Results pane.

Besides PROC IML, two other approaches exist. PROC GATEWAY runs R or Python code and supports multithreaded execution, although all data must sit in CAS libraries. The SWAT package works the other way around, since it is the R client to SAS Cloud Analytic Services, so an R user can connect to Viya and work with data held there.

Having several ways to reach R fits the way SAS programming is often described, since a common remark within the SAS community is that almost any programming problem has more than one solution. Python takes the opposite stance, as its guiding principles in PEP 20 favour a single obvious way to do a task. The same multiplicity appears throughout the sections that follow, where data, values and results can each move between SAS and R in more than one manner.

Among those options, PROC R runs R code directly from a SAS session. Its basic shape places R code between SUBMIT and ENDSUBMIT statements, as the following code, which doubles the MSRP values in SASHELP.CARS, shows. Note that the two-argument form of the transfer functions needs SAS Viya 2026.05 or later.

proc r;
   submit;
      df <- sd2df("cars", "sashelp")
      df$MSRP <- df$MSRP * 2
      df2sd(df, "cars", "work")
   endsubmit;
run;

In the code above, everything between SUBMIT and ENDSUBMIT is ordinary R, while SAS continues to control the rest of the program. Behind the scenes, the R code runs as a subprocess of the SAS session, and a community guide to PROC R explains that consecutive PROC R steps share the same R session. Thus, objects created in one step remain there in the next. Adding restart to the statement gives a clean R environment, while terminate ends the session and releases its resources, which the guide recommends once the R work is finished.

Moving Data and Values Between SAS and R

Two functions transfer data between the languages. According to the first part of the SAS Communities PROC R series, sd2df() (also known as sasdata2dataframe()) sends a SAS dataset to an R data frame using the haven package. The reverse job falls to df2sd() (or dataframe2sasdata()), which writes an R data frame back as a SAS table.

From SAS Viya 2026.05 these functions accept the library as a separate argument, as in sd2df("cars", "sashelp") and df2sd(df, "cars_r", "work"). The older single-argument form, such as sd2df("sashelp.cars"), still appears in the series. A typical exercise filters SASHELP.CARS to Audi vehicles with a tidyverse function, saves the result to the WORK library as AUDI and prints it with PROC PRINT.

Macro variables give a second channel for passing information. In R, symget("Make") reads a SAS macro variable called Make, while symput("Make", "Audi") sets one from R. That allows SAS to supply a filter value and R to calculate the average of city and highway miles per gallon. SAS can then use the answer, held in a macro variable such as AvgMPG, in a report title.

A third link between the two languages is the sasfnc() function, which calls a SAS function from R. It builds a %SYSFUNC call behind the scenes, and the series applies propcase() to the CITY column of SASHELP.HOMEEQUITY to create a CITY2 column with capitalised values. A SAS function can therefore be used without being rewritten in R.

Going the other way, the submit() function hands SAS code back to SAS. The paper and slides for Jim Box's talk at PHUSE EU Connect 2026 use submit("%put &numDead;") to write a macro variable value to the log, and the code runs only when the PROC R step exits. That timing matters whenever R and SAS steps need to alternate, since a later R statement will not see the effect of a queued SAS statement.

Getting Results and Plots Back into SAS

Output from the R print() function goes to the SAS log. The show() function sends an R object to the Results window instead, with options for a title and the number of rows to display, and the Base SAS Procedures Guide page on displaying data frames shows it alongside sd2df() and df2sd(). A call such as show(head(carsAudi), title = "Audi Vehicles") therefore presents the first rows of the filtered data as a table in the Results window.

Graphics follow the same pattern, with rplot() rendering base R or ggplot2 plots in the SAS Results window. Several plots can appear from a single PROC R step, and the series demonstrates this with SASHELP.SHOES by charting sales by region, sales against inventory and a heatmap of sales across regions and product categories. If a plot needs to be saved, rplot() can write it to a file in the SAS work path, and renderImage() displays it later when given the full path.

Keeping R Code in Separate Scripts

Embedding R in a PROC R step suits short analyses, while larger or reusable work can be kept in its own file. According to the third part of the SAS Communities PROC R series, SAS Studio can create a stand-alone R program from the Start page or the New menu. A SAS program then calls that script through the INFILE= option of the PROC R statement.

The script can live in SAS Content or on the SAS server file system, and its location can be given directly or through a fileref created with a FILENAME statement. The fileref name can be no longer than eight characters. The worked script reads SASHELP.CARS, filters to Honda vehicles, builds a highway MPG histogram and saves the filtered data as a SAS table. In a variant, SAS sets a Make macro variable, the script reads it with symget() and the average price returns to SAS through symput(). Data and macro variables therefore still pass between the script and SAS.

Using R Inside Visual Flows

Beyond code, SAS Data and AI Studio flows assemble data operations as steps on a canvas, with connections showing how data moves between them. The fourth part of the SAS Communities PROC R series covers the R Program step. This can run alone or take data from earlier steps, and its code can be typed into the editor or linked to an external R script through the node settings.

The flow in that post prepares SASHELP.HOMEEQUITY with SAS steps that drop rows with missing loan or property values and calculate a loan-to-value ratio. The R step then sorts loans into low, medium and high categories, builds a summary table and draws a scatter plot of loan amount against property value. Finally, it writes the enriched data back to SAS as HOMEEQUITY_LTV, ready for later steps, while the summary and plot show in the flow results.

Mixing Languages in One Workflow

PROC R can be used alongside other languages when a workflow calls for them. A SAS Innovate session on the procedure describes combining it with PROC PYTHON and PROC GATEWAY so that programmers can switch between languages within one process. Shared tables and macro variables link the steps, and the workflow stays inside SAS.

Managing and Checking the R Environment

Access to PROC R depends on how the Viya environment has been set up. Administrators normally prepare R through the SAS Configurator for Open Source, where R builds, and package lists are defined in profiles. Under this arrangement everyone uses the same R version and packages.

For the Compute Server, the Viya deployment notes state that the PROC_RPATH environment variable is used by PROC R. The R version and packages available can therefore be checked from within SAS.

Jim Box's paper shows how to do this using only the pieces already covered. A short PROC R step reads R.version.string and R.home() into macro variables with symput(), then writes installed.packages() to a SAS table with df2sd(). A PROC SQL report can then list the packages under a title built from those macro variables, and the paper's output shows R 4.4.3 with 373 packages. The paper also notes that the administrator provides this as a production R environment, so individual users cannot change the package list or versions.

Alongside the Configurator, Posit offers a separate approach to controlling R packages. The SAS and Posit partnership announced on the 16th November 2026 lists Posit Package Manager among the Posit technologies to be integrated with SAS Viya and SAS Clinical Acceleration, and the release says it helps keep R and Python environments consistent and reproducible. Posit also describes date-based snapshots that fix the package versions available on a given date, and the R Consortium R Submission Pilot 3 used a snapshot from Posit Public Package Manager for a clinical trial submission.

PROC R as One of Several Ways of Using R with SAS

PROC R became available with SAS Viya 2026.03, and later releases added to it: the two-argument transfer functions arrived with 2026.05 and the R Program step in flows is based on the 2026.06 stable release. The R installation and packages that the procedure uses are set by an administrator, so a program written for one site depends on that site having the same R version and packages. Because SAS already offers several ways of working with R, PROC R is one more choice, and it is the one that keeps R code, data, macro variables and output inside a single SAS program or flow.

Add a Comment

Your email address will not be published. Required fields are marked *

Please be aware that comment moderation is enabled and may delay the appearance of your contribution.

  • The content, images, and materials on this website are protected by copyright law and may not be reproduced, distributed, transmitted, displayed, or published in any form without the prior written permission of the copyright holder. All trademarks, logos, and brand names mentioned on this website are the property of their respective owners. Unauthorised use or duplication of these materials may violate copyright, trademark and other applicable laws, and could result in criminal or civil penalties.

  • All comments on this website are moderated and should contribute meaningfully to the discussion. We welcome diverse viewpoints expressed respectfully, but reserve the right to remove any comments containing hate speech, profanity, personal attacks, spam, promotional content or other inappropriate material without notice. Please note that comment moderation may take up to 24 hours, and that repeatedly violating these guidelines may result in being banned from future participation.

  • By submitting a comment, you grant us the right to publish and edit it as needed, whilst retaining your ownership of the content. Your email address will never be published or shared, though it is required for moderation purposes.