Encounter with sasquatch: Using SAS, R, and Quarto together

Ryan Zomorrodi

About me

  • Research Analyst at the Children’s Environmental Health Initiative
  • Graduated with an MS in Biostatistics from the University of Illinois Chicago this May
  • Open source contributor/maintainer of projects such as the CDC Rate Stabilizing ArcGIS Pro Python Toolbox, as well as the R packages sasquatch, healthatlas, and centr.

Why make sasquatch?


To complete my homework.

Take this question:


“Fit a model with random subject intercepts and week effects. Is this model significantly better than the random-intercepts model? Write the two-level model and interpret the parameters in this model.”


To answer this question I must:

  • Run SAS code
  • Include SAS output within my answer
  • Interpret the output
  • Write out the model formula

What were my options?

ODS

I could use PROC ODSTEXT to add my interpretations into the ODS output

Reproducable
No setup

PROC ODSTEXT statements would liter my code
The output wouldn’t include my code
I can’t write formulas

Word document

I could just screenshot my output and add it to a word document

Math notation is supported
Answers do not have to be embeded inside the code

Not reproducable
Tedious

Jupyter notebooks

I could use the SAS jupyter kernel and I probably should have but…

it would be really nice to have

  • The ability to show output AND log
  • Multi-language support

The alternative… Quarto documents

Quarto is a

  • Language-agnostic open-source publishing system
  • Support multiple output formats including: articles, presentations, dashboards, websites, blogs, and books in HTML, PDF, MS Word, ePub, and more

So… what does sasquatch do?

sasquatch serves as a bridge between R and SAS, enabling:

  • Two-way data exchange between SAS and R
  • Rendering of SAS output (HTML or LaTeX) within Quarto or R Markdown documents
  • Interactive execution of SAS code blocks
  • Basic file management on a SAS client

+

+

=

How does sasquatch send data between R and SAS?

library(sasquatch)
sas_connect()

sas_from_r(
  x = mtcars,
  table_name = "mtcars",
  libref = "work"
)
library(reticulate)
saspy <- import("saspy")
session <- saspy$SASsession()

session$df2sd(
  df = r_to_py(mtcars),
  table = "mtcars",
  libref = "work"
)
import saspy

session = saspy.SASsession()

session.df2sd(
  df = r.mtcars, 
  table = "mtcars", 
  libref = "work"
)

It’s not that easy!

sasquatch does quite a few things for you

  • Checks column names and types
  • Provides helpful errors
  • Prevents type casting errors
  • Automatically adds formats

How does sasquatch interface with Quarto?

sasquatch introduces a new custom language knitr engine.

Writing a sasquatch quarto document

example.qmd

---
title: "Example"
format: html
engine: knitr
---

example.html

Writing a sasquatch quarto document

example.qmd

---
title: "Example"
format: html
engine: knitr
---

```{r}
library(sasquatch)
sas_connect()
```

example.html

Writing a sasquatch quarto document

example.qmd

---
title: "Example"
format: html
engine: knitr
---

```{r}
library(sasquatch)
sas_connect()
```

```{sas}
PROC PRINT DATA = sashelp.cars (OBS = 10);
    VAR make model type origin msrp invoice;
RUN;
```

example.html

Writing a sasquatch quarto document

example.qmd

---
title: "Example"
format: pdf
engine: knitr
---

```{r}
library(sasquatch)
sas_connect()
```

```{sas}
PROC PRINT DATA = sashelp.cars (OBS = 10);
    VAR make model type origin msrp invoice;
RUN;
```

example.pdf

Interactive execution

You can use your sasquatch to visualize SAS output in many of R developers’ favorite IDEs.

RStudio

Positron/Visual Studio Code

Basic file management

sasquatch provides file management functions that emulate those from base R

Function Base R Equivalent
sas_list() list.files()
sas_file_exists() file.exists()
sas_file_upload() -
sas_file_download() -
sas_file_copy() file.copy()
sas_file_remove() file.remove()

Next steps

  • Completion of rOpenSci review and transfer to rOpenSci
    • sasquatch will remain supported even if I chose to stop maintaining it
    • rOpenSci opens up new opportunities for promotion (newsletter, blog, social media)
  • Submission to the Comprehensive R Archive Network (CRAN)

Features I’m thinking about:

  • Helper configuration functions for the python-challenged
  • R factors to SAS formated variables
  • sasquatch dir.create() equivalent

Live Demo