Skip to contents

Overview

This section describes alternative software solutions to implement OECDsppps into existing workflows in Python and SAS.

1 Python

1.1 Integrating Python into an R project

An R interface to Python is provided by reticulate; see the packages Getting started guide for more information and the various ways to integrate Python code into an R project.

The next section focuses on one such solution, that is, integrating Python in R Markdown.

1.1.1 Python in R Markdown and Quarto

After successfully installing and (if necessary) configuring Python, Python and R code can be run in the same IDE (recommended are either RStudio or Positron) in an interactive session where data can be moved between both environments interactively using either R Markdown or Quarto.

In step 1, initiate a Python environment in R; the example below uses a generic virtual environment.

```{r}
# In R
library(reticulate)
use_virtualenv("path_to_your_virtual_environment")
```

In step 2, perform computations in Python in a separate code chunk initiated by ```{python} `. In this generic example, a data file with price quotes is loaded and transformed to only keep the variables relevant for the CPD calculation, which will be performed in R in the next step.

```{python}
# In Python
import pandas
## Load price data
prices = pandas.read_csv("prices.csv")
## Only keep relevant variables for CPD estimation
prices_out = prices[["region", "product", "price"]]
```

In step 3, move data object prices_out from Python into R and estimate the CPD method using function estim_cpd() in OECDsppps. Note that this data object can be accessed in R by running py$prices_out.

```{r}
# In R
library(OECDsppps)
results_cpd <- estim_cpd(
        py$prices_out,
        region = "region",
        product = "product",
        price = "price")
```

2 SAS

SAS users can execute R with the R procedure (PROC R) directly inside SAS, while keeping the data in the SAS ecosystem.

PROC R is available to SAS Viya users starting with the 2026.03 SAS Viya release.

A more detailed discussion on this integration can be found in the SAS Communities Library “Introducing PROC R: The Newest Way to Integrate R and SAS” as well as the Base SAS Procedures Guide “What Does the R Procedure Do?”.

Note: A complete installation of R including the following R packages is necessary to have full PROC R functionality: R6, arrow, haven, plotly, svglite, gt. To run OECDsppps, these packages as well as all OECDsppps dependencies must be installed.

2.1 Stylised example: running R in SAS

R can be run from a SAS programme saved in a .sas file by running:

* SAS code;
proc r;
    submit;
    print('This is run in R!');
    endsubmit;
run;

Data loaded into a SAS data set can be passed between R and SAS as described by following these steps; a more detailed step-by-step guide is provided in “Introducing PROC R: The Newest Way to Integrate R and SAS”:

  1. Export a SAS data set to a local R data frame using sd2df or sasdata2dataframe.
  2. Perform calculations in R using R syntax.
  3. Transfer R data to a SAS data set using df2sd or dataframe2sasdata.

2.2 Integrating OECDsppps into a SAS workflow

The generic example below converts a table called price stored in the work library to an R data object and estimates the Country-Product-Dummy (CPD) method using the OECDsppps function estim_cpd(). Then, the retrieved CPD estimates are converted back to a SAS table called cpd stored in the work library.

* SAS code;
proc r;
    submit;
    library(OECDsppps)
    sas_data_to_r_data <- sd2df("sashelp.price")
    results_cpd <- sas_data_to_r_data %>% estim_cpd(
        region = "region",
        product = "product",
        price = "price")
    df2sd(results_cpd, "work.cpd");
    endsubmit;
run;