polarsreadstat reads SAS, Stata and SPSS data files into Polars DataFrames, like haven reads them into tibbles.
library(polarsreadstat)
#> Loading required package: polars
path <- system.file("examples", "iris.sav", package = "haven")
read_readstat(path)
#> shape: (150, 5)
#> ┌──────────────┬─────────────┬──────────────┬─────────────┬───────────┐
#> │ Sepal.Length ┆ Sepal.Width ┆ Petal.Length ┆ Petal.Width ┆ Species │
#> │ --- ┆ --- ┆ --- ┆ --- ┆ --- │
#> │ f64 ┆ f64 ┆ f64 ┆ f64 ┆ str │
#> ╞══════════════╪═════════════╪══════════════╪═════════════╪═══════════╡
#> │ 5.1 ┆ 3.5 ┆ 1.4 ┆ 0.2 ┆ setosa │
#> │ 4.9 ┆ 3.0 ┆ 1.4 ┆ 0.2 ┆ setosa │
#> │ 4.7 ┆ 3.2 ┆ 1.3 ┆ 0.2 ┆ setosa │
#> │ 4.6 ┆ 3.1 ┆ 1.5 ┆ 0.2 ┆ setosa │
#> │ 5.0 ┆ 3.6 ┆ 1.4 ┆ 0.2 ┆ setosa │
#> │ … ┆ … ┆ … ┆ … ┆ … │
#> │ 6.7 ┆ 3.0 ┆ 5.2 ┆ 2.3 ┆ virginica │
#> │ 6.3 ┆ 2.5 ┆ 5.0 ┆ 1.9 ┆ virginica │
#> │ 6.5 ┆ 3.0 ┆ 5.2 ┆ 2.0 ┆ virginica │
#> │ 6.2 ┆ 3.4 ┆ 5.4 ┆ 2.3 ┆ virginica │
#> │ 5.9 ┆ 3.0 ┆ 5.1 ┆ 1.8 ┆ virginica │
#> └──────────────┴─────────────┴──────────────┴─────────────┴───────────┘Features
-
Formats: SAS (
.sas7bdat, transport.xptv5/v8), Stata (.dtaformats 102 to 119) and SPSS (.sav, compressed.zsav, portable.por). -
Reading only what you need: a subset of columns (
col_select) or a range of rows (skip,n_max). -
Proper types: dates, datetimes and times become Polars
Date,Datetime(UTC) andTime, including Stata’s weekly to yearly periods and SPSS durations; Statabyte/int/longkeep their integer widths. -
Value labels become text (
value_labels = TRUE, the default) or keep their codes. SAS value labels are read from a format catalog (catalog_file). -
Missing values: system and extended missing values (
.a–.z) are null; SPSS user-defined missing values are null or kept (user_na). A column of codes tells which missing value each null was (missing_codes). -
Text encodings: SAS and SPSS files declare theirs; old Stata files (before Stata 14) can be read in any code page (
encoding). -
Metadata without reading the data: variable labels, display formats, value labels, user-defined missing values, SPSS measures (
read_readstat_metadata()).
Installation
polarsreadstat is distributed through R-universe and R-multiverse, like polars. R-universe and R-multiverse have binaries for Windows, macOS and Ubuntu.
install.packages(
"polarsreadstat",
repos = c(
"https://yousa-mirage.r-universe.dev",
"https://community.r-multiverse.org"
)
)You can also install from source with pak:
pak::pak("Yousa-Mirage/jiebaRS")Note: Building from source requires Rust tool chain to compile the Rust backend. And it will cost much more time to compile than using the pre-compiled binaries.
Usage
read_readstat() picks the reader from the file extension; read_dta(), read_sas(), read_xpt(), read_sav() and read_por() read one format whatever the extension. As in haven, read_stata() is read_dta(), and read_spss() reads .sav, .zsav and .por files automatically.
read_spss(path)
#> shape: (150, 5)
#> ┌──────────────┬─────────────┬──────────────┬─────────────┬───────────┐
#> │ Sepal.Length ┆ Sepal.Width ┆ Petal.Length ┆ Petal.Width ┆ Species │
#> │ --- ┆ --- ┆ --- ┆ --- ┆ --- │
#> │ f64 ┆ f64 ┆ f64 ┆ f64 ┆ str │
#> ╞══════════════╪═════════════╪══════════════╪═════════════╪═══════════╡
#> │ 5.1 ┆ 3.5 ┆ 1.4 ┆ 0.2 ┆ setosa │
#> │ 4.9 ┆ 3.0 ┆ 1.4 ┆ 0.2 ┆ setosa │
#> │ 4.7 ┆ 3.2 ┆ 1.3 ┆ 0.2 ┆ setosa │
#> │ 4.6 ┆ 3.1 ┆ 1.5 ┆ 0.2 ┆ setosa │
#> │ 5.0 ┆ 3.6 ┆ 1.4 ┆ 0.2 ┆ setosa │
#> │ … ┆ … ┆ … ┆ … ┆ … │
#> │ 6.7 ┆ 3.0 ┆ 5.2 ┆ 2.3 ┆ virginica │
#> │ 6.3 ┆ 2.5 ┆ 5.0 ┆ 1.9 ┆ virginica │
#> │ 6.5 ┆ 3.0 ┆ 5.2 ┆ 2.0 ┆ virginica │
#> │ 6.2 ┆ 3.4 ┆ 5.4 ┆ 2.3 ┆ virginica │
#> │ 5.9 ┆ 3.0 ┆ 5.1 ┆ 1.8 ┆ virginica │
#> └──────────────┴─────────────┴──────────────┴─────────────┴───────────┘Labelled values come back as their labels. Set value_labels = FALSE to keep the codes, and use col_select, skip and n_max to read part of a file:
read_sav(
path,
col_select = c("Species", "Petal.Width"),
value_labels = FALSE,
skip = 50,
n_max = 3
)
#> shape: (3, 2)
#> ┌─────────┬─────────────┐
#> │ Species ┆ Petal.Width │
#> │ --- ┆ --- │
#> │ f64 ┆ f64 │
#> ╞═════════╪═════════════╡
#> │ 2.0 ┆ 1.4 │
#> │ 2.0 ┆ 1.5 │
#> │ 2.0 ┆ 1.5 │
#> └─────────┴─────────────┘The result is a Polars DataFrame, so the whole Polars API applies:
read_readstat(path)$group_by(
"Species",
.maintain_order = TRUE
)$agg(
pl$col("Petal.Width")$mean()
)
#> shape: (3, 2)
#> ┌────────────┬─────────────┐
#> │ Species ┆ Petal.Width │
#> │ --- ┆ --- │
#> │ str ┆ f64 │
#> ╞════════════╪═════════════╡
#> │ setosa ┆ 0.246 │
#> │ versicolor ┆ 1.326 │
#> │ virginica ┆ 2.026 │
#> └────────────┴─────────────┘tidypolars provides a dplyr-like interface to Polars and it works with polarsreadstat:
library(dplyr)
library(tidypolars)
read_readstat(path) |>
group_by(Species, maintain_order = TRUE) |>
summarize(mean_petal_width = mean(Petal.Width))
#> shape: (3, 2)
#> ┌────────────┬──────────────────┐
#> │ Species ┆ mean_petal_width │
#> │ --- ┆ --- │
#> │ str ┆ f64 │
#> ╞════════════╪══════════════════╡
#> │ setosa ┆ 0.246 │
#> │ versicolor ┆ 1.326 │
#> │ virginica ┆ 2.026 │
#> └────────────┴──────────────────┘Use as.data.frame() (or tibble::as_tibble()) to get an R data frame.
Read Metadata
read_readstat_metadata() reads what the file says about its variables without reading the data. Each reader has a metadata counterpart: read_dta_metadata(), read_sav_metadata(), read_spss_metadata() and so on.
meta <- read_readstat_metadata(path)
meta
#> <readstat_metadata> SPSS file
#> Rows: 150
#> Encoding: UTF-8
#> Variables: 5, 1 with value labels, 0 with user-defined missing values
#>
#> name type format label
#> Sepal.Length numeric F8.2 <NA>
#> Sepal.Width numeric F8.2 <NA>
#> Petal.Length numeric F8.2 <NA>
#> Petal.Width numeric F8.2 <NA>
#> Species numeric F8.0 <NA>meta$variables is a data frame with a row per variable, named after it. Value labels and SPSS user-defined missing values are in named lists:
meta$variables["Species", c("format", "measure")]
#> format measure
#> Species F8.0 nominal
meta$value_labels$Species
#> setosa versicolor virginica
#> 1 2 3Format-specific options
For SAS, value labels live in a separate format catalog and you can specify it with catalog_file to read.
read_sas("data.sas7bdat", catalog_file = "formats.sas7bcat")For SPSS, you can keep user-defined missing values (e.g. 99 = “refused”) as values with user_na = TRUE.
read_sav("survey.sav", user_na = TRUE)Or keep them null and add a column <name>_missing with the code of each missing value: the value itself in SPSS, ".a" to ".z" for Stata’s special missing values and ".A" to ".Z" or "._" for SAS’s.
read_dta("survey.dta", missing_codes = TRUE)For Stata, files from before Stata 14 don’t record their text encoding and are read as Windows-1252 by default, the same as in haven. For files written elsewhere, e.g. on a Chinese system, you can specify it with encoding.
read_dta("old_chinese.dta", encoding = "GBK")Why there are no scan_*() functions
The Python package polars_readstat has lazy scan_*() functions. They are built on Python Polars’ IO plugins. But r-polars has no such interface because Polars’ IO plugins rely on Python callbacks, so one is unlikely to come (see r-polars#1402).
Without it, a scan_*() function in polarsreadstat could only read the whole file and wrap it in a LazyFrame, which would look lazy while giving none of the savings. Instead, the readers take the pushdown as arguments: choose the columns with col_select and the rows with skip and n_max, then call $lazy() on the result if you want a lazy query:
Differences from haven
polarsreadstat aims to read the same data as haven, and the tests confirm this. The test files are 572 SAS, Stata and SPSS files, from polars_readstat, written by many programs and versions, from Stata 104 to 119 and from 32-bit SAS to compressed SPSS files.
After converting both into R data frames, 563 files read the identical values column by column as haven. The other 9 differ only in the ways listed below, and their tests check that the rest of the file still matches. And the metadata of all files is the same too.
Where polarsreadstat and haven differ, it is on purpose:
haven |
polarsreadstat |
|
|---|---|---|
| Result | tibble | Polars DataFrame |
| Labelled values |
labelled vectors (codes + labels) |
labels as text, or codes |
Stata %tw, %tm, %tq, %th, %ty
|
period counts | dates (start of the period) |
SAS MONNAME formats |
day counts | dates |
| Stata formats 102 and 103 | error | read |
| SAS files with no variables | error | empty DataFrame |
| Empty strings | "" |
null (empty_string_as_null = FALSE keeps "") |
Benchmark
Median time in seconds to read whole files written by haven, on Apple M5, with R version 4.6.2 (2026-10-29), polarsreadstat 0.2.0, polars 2.1.0 and haven 2.6.0:
| File | Size (MB) | Rows × columns | haven | polarsreadstat | Speedup | + as.data.frame() |
|---|---|---|---|---|---|---|
| mixed.dta | 179 | 2,000,000 × 18 | 2.82 | 0.08 | 35× | 1.94 |
| mixed.sas7bdat | 260 | 2,000,000 × 18 | 2.69 | 0.04 | 61× | 1.45 |
| mixed.sav | 287 | 2,000,000 × 18 | 2.86 | 0.11 | 26× | 1.93 |
| mixed.xpt | 257 | 2,000,000 × 18 | 3.34 | 0.07 | 51× | 1.42 |
| mixed.zsav | 169 | 2,000,000 × 18 | 3.11 | 0.19 | 16× | 2.05 |
| strings.dta | 549 | 1,000,000 × 4 | 1.46 | 0.11 | 13× | 1.30 |
| strings.sas7bdat | 558 | 1,000,000 × 4 | 1.43 | 0.06 | 25× | 1.17 |
| strings.sav | 538 | 1,000,000 × 4 | 2.89 | 0.17 | 17× | 1.43 |
| wide.dta | 297 | 20,000 × 2,000 | 2.10 | 0.12 | 17× | 0.88 |
| wide.sas7bdat | 313 | 20,000 × 2,000 | 2.23 | 0.08 | 29× | 0.57 |
| wide.sav | 333 | 20,000 × 2,000 | 2.41 | 0.21 | 12× | 0.92 |
polarsreadstat is 12 to 61 times faster than haven at reading a file into a DataFrame, and 3 to 31 times faster at reading 3 of its columns. Converting the DataFrame to an R data frame is done by Polars and dominated by creating R strings; numeric data converts almost for free.
mixed is typical survey data (numbers, integer codes, short strings, a date, a labelled column); wide has 2,000 columns; strings has 3 long text columns. For more information about the benchmark, see bench/.
Acknowledgements
- The underlying file parsers come from Jon Rothbaum’s
polars-readstat-rscrate, a pure-Rust reimplementation ofReadStat.polarsreadstatvendors a modified copy of the crate with many bug fixes and additions (seesrc/rust/patches/), along with its test suite. -
ReadStat by Evan Miller, the C library behind
haven,pandasandpyreadstat, documents these file formats. -
havendefines the standard behavior of reading these files in R, and is whatpolarsreadstatis tested against. - r-polars and nanoarrow receive the data; savvy connects R and Rust.