Pyreadstat: Read/Write SAS, SPSS, Stata Files

1.3.3 · active · verified Sun Apr 12

Pyreadstat is a Python library that allows reading and writing SAS (.sas7bdat, .xpt), SPSS (.sav, .zsav), and Stata (.dta) files into/from pandas and polars data frames. It is currently at version 1.3.3 and maintains an active, though irregular, release cadence to adapt to new pandas/polars versions and add features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import pyreadstat, write a simple pandas DataFrame to a SAS .sas7bdat file, and then read it back, illustrating basic data and metadata retrieval. The same `read_*` and `write_*` patterns apply to SPSS and Stata files.

import pyreadstat
import pandas as pd
import os

# Example: Create a dummy SAS file for reading and writing
data = {'col1': [1, 2, 3], 'col2': ['A', 'B', 'C']}
df_to_write = pd.DataFrame(data)
output_file = 'test_sas_file.sas7bdat'

# Write a dummy SAS file
try:
    pyreadstat.write_sas7bdat(df_to_write, output_file)
    print(f"Dummy SAS file '{output_file}' created successfully.")

    # Read the SAS file
    df_read, meta = pyreadstat.read_sas7bdat(output_file)

    print("\nDataFrame read from file:")
    print(df_read)
    print("\nMetadata read from file:")
    print(f"Column Names: {meta.column_names}")
    print(f"Column Labels: {meta.column_labels}")
    print(f"Table Name: {meta.table_name}")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    if os.path.exists(output_file):
        os.remove(output_file)
        print(f"Cleaned up '{output_file}'.")

view raw JSON →