Package 'polarsreadstat'

Title: Fast Reading of SAS, Stata and SPSS Files into a Polars DataFrame
Description: Reads 'SAS', 'Stata' and 'SPSS' files into 'Polars' DataFrames using a multi-threaded Rust parser.
Authors: Hao Cheng [aut, cre, cph]
Maintainer: Hao Cheng <[email protected]>
License: MIT + file LICENSE
Version: 0.2.1
Built: 2026-12-06 08:10:36 UTC
Source: https://github.com/Yousa-Mirage/PolarsReadStat

Help Index


Read SAS, Stata and SPSS files into a Polars DataFrame

Description

Read data files from SAS (.sas7bdat, .xpt), Stata (.dta) and SPSS (.sav, .zsav, .por) into a Polars DataFrame, like haven reads them into tibbles. You can read only some columns or a range of rows, and choose whether labelled values come back as their labels or as the stored codes.

read_readstat() will detect the format from the file extension, while read_dta(), read_sas(), read_xpt(), read_sav() and read_por() read the specified format.

Usage

read_readstat(
  path,
  col_select = NULL,
  skip = 0,
  n_max = NULL,
  value_labels = TRUE,
  empty_string_as_null = TRUE,
  user_na = FALSE,
  missing_codes = FALSE,
  catalog_file = NULL,
  encoding = NULL,
  threads = NULL,
  format = NULL
)

read_dta(path, ...)

read_sas(path, ...)

read_xpt(path, ...)

read_sav(path, ...)

read_por(path, ...)

read_stata(path, ...)

read_spss(path, ...)

Arguments

path

Path to the file.

col_select

Character vector of column names to read, in the order they should appear. NULL (default) reads all columns.

skip

Number of rows to skip before reading.

n_max

Maximum number of rows to read. NULL (default) reads all rows.

value_labels

If TRUE (default), labelled values are returned as their labels; if FALSE, the stored codes are kept. See Details.

empty_string_as_null

If TRUE (default), empty strings are read as null; if FALSE, they are kept as "". See Details.

user_na

Only for SPSS files. If TRUE, SPSS user-defined missing values are kept as values; if FALSE (default), they are read as null. See Details.

missing_codes

Not for SAS transport and SPSS portable files. TRUE to add a column of missing value codes after each column that can have special or user-defined missing values (numeric Stata and SAS columns, SPSS columns with user-defined missing values), a character vector of column names to add one after those columns only, or FALSE (default) for none. Can't be used with user_na = TRUE. See Details.

catalog_file

Only for SAS files. Path to a SAS format catalog (.sas7bcat) holding the value labels of a SAS data file, or NULL (default). See Details.

encoding

Only for Stata files. The text encoding of a Stata file, as a label such as "latin1", "GBK" or "UTF-8", or NULL (default) to use the format's: UTF-8 from Stata 14 on, Windows-1252 before. See Details.

threads

Number of decoding threads. NULL (default) picks one per physical core.

format

One of "sas", "xpt", "stata", "spss" (.sav, .zsav) or "por" (SPSS portable). NULL (default) detects it from the extension.

...

Passed on to read_readstat().

Details

Column types

Stored in the file Polars type
Stata byte / int / long Int8 / Int16 / Int32
Stata long in files older than format 113 Int64
Stata float / double Float32 / Float64
SAS and SPSS numbers Float64
Strings, including Stata strLs String
Dates: SAS date formats, Stata ⁠%td⁠, SPSS date formats Date
Stata ⁠%tw⁠, ⁠%tm⁠, ⁠%tq⁠, ⁠%th⁠, ⁠%ty⁠ Date, the first day of the period
Datetimes: SAS datetime formats, Stata ⁠%tc⁠/⁠%tC⁠, SPSS DATETIME Datetime in UTC
Times of day: SAS time formats, SPSS TIME Time
SPSS DTIME Duration

Datetimes are stored as clock times without a time zone. They are returned in UTC, like haven, so the clock time stays the same whatever R's time zone is. Their unit is milliseconds, or microseconds for SAS. Fractional seconds are kept. A Stata ⁠%tc⁠ column is a datetime even when its display format shows only the time of day.

In Stata files older than format 113, long -2147483648 is a valid value, not a missing one. R uses that number for NA_integer_, so these columns are Int64, which keeps the value when converted to R.

Missing values

Numeric missing values become null: system missing values, and the special missing values .a to .z in Stata and .A to .Z and ._ in SAS.

SPSS user-defined missing values (discrete values and ranges, for numbers and strings) also become null by default. Set user_na = TRUE to keep them as values, like haven::read_sav(user_na = TRUE); with value_labels = TRUE, a kept missing value that has a label becomes its label.

Set missing_codes to also learn which missing value each null was. Each column it names gets a String column ⁠<name>_missing⁠ right after it, holding the code of each special or user-defined missing value: ".a" in Stata, ".A" in SAS, and the value itself in SPSS (e.g. "-99"). It is null where the value isn't missing or is a system missing value. This is what haven keeps in tagged NAs (Stata, SAS) and with user_na = TRUE (SPSS). read_readstat_metadata() lists each variable's missing values and the labels of their codes (missing_labels for Stata and SAS, value_labels for SPSS).

These formats have no missing value for strings and store an empty string instead, so empty strings are read as null by default. Set empty_string_as_null = FALSE to keep them as "", like haven does. Strings of spaces are data and are always kept.

Value labels

With value_labels = TRUE, a column that has value labels becomes a String column. Labelled values are replaced by their labels, and values without a label are written out as text (2 becomes "2"), like haven::as_factor().

SAS keeps value labels in a separate format catalog (.sas7bcat). Pass it as catalog_file to label SAS columns: a column is labelled when the catalog has a format of the same name as the column's display format, as in haven::read_sas(). Without a catalog, SAS columns keep their codes.

Text encoding

Text is returned as UTF-8. SAS and SPSS files, and Stata files from Stata 14 on (format 118 and later), record their encoding. Older Stata files don't: they hold text in the code page of the system that wrote them, read as Windows-1252 (Latin-1, the code page of Western Windows systems) as in haven, ReadStat and pandas. For a file written elsewhere, e.g. on a Chinese or Japanese system, set encoding to its code page ("GBK", "Big5", "Shift_JIS", ...). Some programs write UTF-8 into old formats (e.g. haven::write_dta(version = 13)): if accented letters come out as pairs of odd characters, use encoding = "UTF-8". Bytes that aren't valid in the encoding become the replacement character (U+FFFD).

Deleted SAS rows

Rows that SAS marked as deleted are dropped, as SAS itself (and haven) does.

Value

A Polars DataFrame.


Read the metadata of a SAS, Stata or SPSS file

Description

read_readstat_metadata() reads what a file says about its data (variable labels, display formats, value labels, user-defined missing values) without reading the data itself.

Usage

read_readstat_metadata(
  path,
  format = NULL,
  catalog_file = NULL,
  encoding = NULL
)

read_dta_metadata(path, ...)

read_sas_metadata(path, ...)

read_xpt_metadata(path, ...)

read_sav_metadata(path, ...)

read_por_metadata(path, ...)

read_stata_metadata(path, ...)

read_spss_metadata(path, ...)

Arguments

path

Path to the file.

format

One of "sas", "xpt", "stata", "spss" (.sav, .zsav) or "por" (SPSS portable). NULL (default) detects it from the extension.

catalog_file

Only for SAS files. Path to a SAS format catalog (.sas7bcat) holding the value labels of a SAS data file, or NULL (default). With it, value_labels lists the labels of the columns whose display format the catalog defines.

encoding

Only for Stata files. The text encoding of a Stata file, or NULL (default), as for read_readstat().

...

Passed on to read_readstat_metadata().

Details

read_readstat_metadata() will detect the format from the file extension, while read_dta_metadata(), read_sas_metadata(), read_xpt_metadata(), read_sav_metadata() and read_por_metadata() read the specified format.

Value

A readstat_metadata object: a list with

  • format: the file format, as for the format argument.

  • n_rows: the number of rows read_readstat() returns (for SAS, rows marked as deleted aren't counted), or NA if the file doesn't record it (SAS transport and SPSS portable files). For SPSS files whose header doesn't record it, the rows are counted, which reads the whole file.

  • label: the label of the whole dataset, or NA.

  • encoding: the character encoding the file's text is read in (for Stata, encoding or the format's default), or NA if unknown (SAS transport and SPSS portable files).

  • variables: a data frame with one row per variable, named after it: name, label, display format, type ("numeric" or "character"), and for SPSS measure, display_width and alignment. Values a file doesn't have are NA.

  • value_labels: a named list with an element per variable that has value labels: its labelled values, named by their labels, like the "labels" attribute of haven. Values are numbers for numeric variables and strings for character ones.

  • missing_labels: a named list with an element per Stata or SAS variable whose missing values have labels: their codes (".", and ".a" to ".z" in Stata, ".A" to ".Z" and "._" in SAS), named by their labels. haven keeps these in "labels" too, on tagged NAs.

  • na_values, na_range: named lists with an element per SPSS variable that has user-defined missing values: its discrete missing values, and its missing range as c(low, high) (-Inf and Inf for LO and HI), like the attributes of the same names in haven.

Examples

path <- system.file("examples", "iris.sav", package = "haven")
if (nzchar(path)) {
  meta <- read_readstat_metadata(path)
  meta
  meta$variables["Species", "label"]
  meta$value_labels$Species
}