Skip to main content
v2026.11,986 entries · CC-BY 4.0

pyreadstat: Read SPSS, Stata and SAS Files in Python

How to install pyreadstat and read labelled SPSS, Stata and SAS files into pandas, with metadata fields, missing values and fixes for common errors.

Written and maintained by CASRAI Editorial Board

Published

On this page

pyreadstat is a Python package that reads and writes SAS, SPSS and Stata data files and returns the data as a pandas or polars dataframe together with a metadata object holding variable labels and value labels. Install it with pip install pyreadstat and call a reader such as pyreadstat.read_sav(), read_dta() or read_sas7bdat().

What pyreadstat is used for in research

Many survey, cohort and administrative datasets are distributed as SPSS (.sav), Stata (.dta) or SAS (.sas7bdat) files, and those files carry more than numbers: variable labels, value labels and user-defined missing codes. According to the PyPI project page and the official documentation (both checked 2026-11-18), pyreadstat is a wrapper around the ReadStat C library that reads SAS (sas7bdat, sas7bcat, xport), SPSS (sav, zsav, por) and Stata (dta) files into pandas or polars dataframes, and writes several of those formats back out. It is released under the Apache-2.0 licence and developed in the Roche/pyreadstat repository on GitHub.

For a research workflow, the practical value is that you can move a labelled dataset from a statistics package into a Python pipeline without first exporting to CSV, which would discard the labels. The same data-reading job in R is covered in how to load data into R; if your data originates in SPSS, see how to analyse data in SPSS, and for Stata and SAS see Stata code and SAS for statistical analysis.

File formats and functions at a glance

The data table below lists, for each file format, the pyreadstat reader and writer functions named in the documentation and the corresponding functions of haven, the R package built on the same C library. Every pyreadstat reader returns a tuple of a dataframe and a metadata object.

How to install or access pyreadstat

The PyPI page gives the standard route, and the project README gives the conda route.

  1. With pip: pip install pyreadstat. Pre-built wheels are published for Windows, macOS and Linux, and the PyPI page lists CPython 3.10 to 3.15.
  2. With conda: conda install -c conda-forge pyreadstat.
  3. Make sure a dataframe library is present. The README states that pyreadstat needs numpy and narwhals, plus pandas or polars.

Building from source is only needed when no wheel matches your platform. The README says a source build needs a C compiler and cython 3.0.0 or later, and that it uses iconv and zlib. The environment variable PYREADSTAT_LINK_ICONV can be set to 1 or 0 to control iconv linking. Python 2.7 has not been supported since version 1.2.3.

pandas can also call pyreadstat for you. The pandas documentation for read_spss says it uses the pyreadstat library, which must be installed separately, and that extra keyword arguments are passed through to pyreadstat.read_sav().

How to read a labelled SPSS, Stata or SAS file step by step

The example below reads a labelled survey file. The file name is illustrative; the parameters are those documented in the README and the official documentation.

  1. Import the package: import pyreadstat.
  2. Read the file and keep both return values: df, meta = pyreadstat.read_sav("survey.sav", apply_value_formats=True, formats_as_category=True, formats_as_ordered_category=False). For Stata use read_dta; for SAS use read_sas7bdat; for SAS catalog files use read_sas7bcat; for transport files use read_xport; for SPSS portable files use read_por.
  3. Inspect the labels before analysis: meta.column_names_to_labels maps each variable name to its label, and meta.variable_value_labels holds the value labels per variable.
  4. Read only what you need. The documentation lists usecols to select columns, row_limit and row_offset to select rows, and metadataonly to read just the metadata without the data.
  5. Choose the output type with output_format: pandas is the default, and polars or a plain dictionary are the documented alternatives.
  6. For very large files, the documentation describes read_file_in_chunks and read_file_multiprocessing. On Windows, the README notes that multiprocessing code must sit under an if __name__ == "__main__": guard.
  7. To write a file back out, use write_sav, write_dta, write_sas7bdat, write_xport or write_por. The writers accept column_labels and variable_format, and the documentation lists missing_ranges or missing_user_values for user-defined missing values.

Value labels versus categories

By default the raw codes are returned and the labels stay in the metadata. The README states that apply_value_formats defaults to False and formats_as_category defaults to True. In practice this means a survey item stored as 1, 2, 3 stays numeric unless you set apply_value_formats=True; once you do, the column holds the label text, as a pandas category by default. Keep the numeric codes when you need to merge with another wave of the same survey, and apply labels when you need readable tables or plots. You can always rebuild the mapping from meta.variable_value_labels.

How to read the pyreadstat output

The second return value is the metadata object. According to the official documentation, it exposes the following attributes.

Metadata attribute What it holds
column_names, column_labels Variable names, and the human-readable label for each
column_names_to_labels Dictionary from variable name to label
variable_value_labels, value_labels Value labels per variable, and the label sets themselves
variable_to_label Which label set each variable uses
original_variable_types, readstat_variable_types Variable types as stored in the source file and as interpreted by ReadStat
variable_measure Measurement level recorded in the file
number_rows, number_columns Dimensions; number_rows can be None for POR and XPORT files read with metadata only
file_label, table_name, file_encoding, creation_time, modification_time, notes File-level descriptive information
missing_ranges User-defined missing ranges, for SPSS
missing_user_values User-defined missing values, for SAS and Stata
mr_sets Multiple-response sets

Missing values

Statistics packages let authors define special missing codes such as 98 for refused. The documentation separates the two mechanisms by format: missing_ranges is reported for SPSS files and missing_user_values for SAS and Stata files. The user_missing parameter on the readers controls how these are handled, so check the metadata for these attributes before computing means on a survey file, and recode deliberately rather than assuming the codes were converted. The README adds one format-specific point: empty strings in SPSS files stay as empty strings and are not converted to NaN.

Dates and encodings

The disable_datetime_conversion parameter turns off date conversion, and the README notes that dates_as_pandas_datetime=True restores the original pandas date behaviour (pandas output only). Text encoding is set with encoding, which takes an iconv encoding name. The README says this is needed for old XPORT files, and that portable (POR) files are assumed to be UTF-8.

Troubleshooting pyreadstat

The README documents the following known limits and fixes.

Symptom Documented cause and fix
OverflowError: date value out of range A date in the file falls outside the range pandas can hold. Set disable_datetime_conversion, or skip the column with usecols.
Text decoding problems in an old XPORT file Pass the correct iconv encoding name with encoding.
Missing _iconv symbol on macOS Install libiconv through conda, then rebuild with pip --no-cache-dir.
pyreadstat cannot read a sas7bdat file it wrote A stated limitation: files written by write_sas7bdat are not readable by pyreadstat itself.
Multiprocessing fails on Windows Put the call under an if __name__ == "__main__": guard.
Empty SPSS strings are not NaN Expected behaviour: they stay as empty strings, so recode them yourself if needed.

The README also says pyreadstat is not a validated package and advises against using it for critical tasks such as reporting to authorities. For regulated submissions, treat the output as something to be checked against the source system rather than a validated conversion.

Free alternatives to pyreadstat

Each of these is documented by its own project. Pick by the language you work in and the formats you need.

Tool Language What its documentation says
pandas read_spss Python Loads an SPSS .sav file; relies on pyreadstat being installed; convert_categoricals (default True) turns value-labelled variables into categoricals; usecols selects columns
pandas read_stata Python Reads Stata .dta files; convert_categoricals defaults to True; chunksize or iterator returns a StataReader for chunked reading
haven R Reads SAS, SPSS and Stata files through the same ReadStat library; value labels become labelled() vectors that as_factor() converts to factors; install with install.packages("haven")

pandas documents that categories read through a Stata iterator may differ between chunks, because a .dta file’s value labels can cover only a subset of the values. If you are comparing R and Python for this job, the SPSS versus R comparison covers the wider tooling choice, and the SAS-side import route is in PROC IMPORT in SAS.

Sources

Data: Statistical file formats and the pyreadstat and haven functions that read and write them

Compares, for each SAS, SPSS and Stata file format, the pyreadstat read and write functions named in its official documentation with the haven read and write functions named on the haven project site. Compiled from the pyreadstat documentation and the haven home page; a dash means the source lists no such function.

Statistical file formats and the pyreadstat and haven functions that read and write them
File formatpyreadstat read functionpyreadstat write functionhaven read functionhaven write function
SAS data (.sas7bdat)read_sas7bdatwrite_sas7bdatread_sas-
SAS catalog (.sas7bcat)read_sas7bcat-read_sas-
SAS transport (.xpt)read_xportwrite_xportread_xptwrite_xpt
SPSS data (.sav)read_savwrite_savread_savwrite_sav
SPSS portable (.por)read_porwrite_porread_por-
Stata data (.dta)read_dtawrite_dtaread_dtawrite_dta

Sources

Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · full access

Ask about pyreadstat: Read SPSS, Stata and SAS Files in Python

Full access: large monthly usage, up to 10 source passages behind each answer, and MCP and API access for Claude Code, Codex and other CLIs. Cancel any time.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →