HOWTO · Pandas
How to Read SAS in Pandas
Read a SAS data file with Python or Pandas, inspect its rows, select columns with pyreadstat, and export the result to CSV.
On this page
To read a SAS dataset in Python, pass its .sas7bdat path to pyreadstat.read_sas7bdat or Pandas’ pd.read_sas. Both routes return tabular data you can inspect; pyreadstat also returns metadata and supports selecting columns with usecols. After loading the data, call to_csv to export it as CSV. These steps read and export data; they do not write a SAS-format file.
What are SAS files
SAS (Statistical Analysis System) is software used for statistical analysis and data management. A .sas7bdat file stores a SAS dataset. A .sas7bcat file is a catalog, which stores formats and related metadata; it is not the dataset to pass to these table-reading examples.
Use the .sas7bdat file when you want to load observations into Python. The examples below use path/to/your-file.sas7bdat as a placeholder. Replace it with the path to your own dataset, relative to the current working directory or as an absolute path.
Open .SAS Files in Python
Install pyreadstat in the same Python environment that runs your script: python -m pip install pyreadstat. The package provides pyreadstat.read_sas7bdat, which returns a pair: a DataFrame and a metadata object. Assign both values so you can inspect the rows and, if needed, metadata such as column labels.
For a dataset at your chosen path, call df, meta = pyreadstat.read_sas7bdat("path/to/your-file.sas7bdat"), then inspect it with df.head(). This is a usage template that requires the installed package and your file; it is not a self-contained run. head() displays the first five rows by default; use df.head(n) to request a different number.
Read SAS File Using Pandas in Python
If Pandas is already part of your workflow, its pd.read_sas function reads a SAS dataset into a DataFrame. Install Pandas in the active environment if needed, then provide the dataset path. This route returns the DataFrame directly.
Use df = pd.read_sas("path/to/your-file.sas7bdat") to load it, then inspect the result with df.head(). Like the previous example, this needs your own .sas7bdat file. If the data uses a non-default text encoding, consult the file’s source or producer before setting the reader’s encoding argument; the correct value depends on the dataset.
Read Specific Columns From the SAS File in Python
When you need only selected columns, pass their names through usecols to pyreadstat.read_sas7bdat. This option belongs to the pyreadstat API shown here; do not assume the same argument is accepted by pd.read_sas.
For example, set columns = ["YEAR"] and call df, meta = pyreadstat.read_sas7bdat("path/to/your-file.sas7bdat", usecols=columns), then inspect the returned DataFrame with df.head(). Replace YEAR with a column name present in your dataset. If you are unsure of the available names, first load the dataset without usecols and inspect df.columns.
Save the SAS Files to CSV in Python
To export the loaded DataFrame, use to_csv and set index=False when the DataFrame index is not a data column you want in the file. The following small standard-library example shows the CSV text produced for two rows. It demonstrates CSV formatting; for a SAS dataset, call to_csv on the DataFrame you loaded above.
Run this example:
import csv
import io
rows = [("year", "sales"), ("2023", "12")]
output = io.StringIO()
writer = csv.writer(output, lineterminator="\n")
writer.writerows(rows)
print(output.getvalue(), end="")
year,sales
2023,12
For the actual DataFrame, write df.to_csv("output.csv", index=False). Choose a destination you can write to; a relative filename is created in the process’s current working directory. The export contains the DataFrame’s columns and rows, without adding its index as an extra column.