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.
- 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. - With conda:
conda install -c conda-forge pyreadstat. - 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.
- Import the package:
import pyreadstat. - 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 useread_dta; for SAS useread_sas7bdat; for SAS catalog files useread_sas7bcat; for transport files useread_xport; for SPSS portable files useread_por. - Inspect the labels before analysis:
meta.column_names_to_labelsmaps each variable name to its label, andmeta.variable_value_labelsholds the value labels per variable. - Read only what you need. The documentation lists
usecolsto select columns,row_limitandrow_offsetto select rows, andmetadataonlyto read just the metadata without the data. - Choose the output type with
output_format: pandas is the default, and polars or a plain dictionary are the documented alternatives. - For very large files, the documentation describes
read_file_in_chunksandread_file_multiprocessing. On Windows, the README notes that multiprocessing code must sit under anif __name__ == "__main__":guard. - To write a file back out, use
write_sav,write_dta,write_sas7bdat,write_xportorwrite_por. The writers acceptcolumn_labelsandvariable_format, and the documentation listsmissing_rangesormissing_user_valuesfor 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
- Python Package Index: pyreadstat, checked 2026-11-18
- pyreadstat documentation, checked 2026-11-18
- Roche/pyreadstat README, GitHub, checked 2026-11-18
- pandas documentation: pandas.read_spss, checked 2026-11-18
- pandas documentation: pandas.read_stata, checked 2026-11-18
- Tidyverse: haven, checked 2026-11-18
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.
| File format | pyreadstat read function | pyreadstat write function | haven read function | haven write function |
|---|---|---|---|---|
| SAS data (.sas7bdat) | read_sas7bdat | write_sas7bdat | read_sas | - |
| SAS catalog (.sas7bcat) | read_sas7bcat | - | read_sas | - |
| SAS transport (.xpt) | read_xport | write_xport | read_xpt | write_xpt |
| SPSS data (.sav) | read_sav | write_sav | read_sav | write_sav |
| SPSS portable (.por) | read_por | write_por | read_por | - |
| Stata data (.dta) | read_dta | write_dta | read_dta | write_dta |
Sources
- pyreadstat documentation (accessed 2026-11-18)
- haven: Import and export SPSS, Stata and SAS files (accessed 2026-11-18)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







