Install & Compatibility
Where this runs
tested against v2.2.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.044s · 19.3MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 2.5s · import 0.037s · 20MB
17MB installed
● package 17MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
SAS7BDAT
✓ from sas7bdat import SAS7BDAT
✗ import sas7bdat.SAS7BDAT
The primary class for reading SAS files is directly importable from the top-level package.
Reads a `.sas7bdat` file, automatically inferring metadata, and converts it into a pandas DataFrame. Emphasizes the use of a context manager and the importance of specifying the correct file encoding.
import os
import pandas as pd
from sas7bdat import SAS7BDAT
# For demonstration, ensure a 'sample.sas7bdat' file exists or provide a path
# You can often find sample .sas7bdat files online or create dummy ones for testing.
# Replace with your actual file path or set SAS_FILE_PATH environment variable.
file_path = os.environ.get('SAS_FILE_PATH', 'sample.sas7bdat')
try:
if not os.path.exists(file_path):
print(f"Warning: '{file_path}' not found. Quickstart cannot run without a SAS file.")
print("Please provide a .sas7bdat file or set the SAS_FILE_PATH environment variable.")
else:
# It's crucial to specify the correct encoding for your SAS file.
# 'latin-1', 'cp1252', or 'utf-8' are common choices.
with SAS7BDAT(file_path, encoding='latin-1') as reader:
df = reader.to_data_frame()
print(f"Successfully read {len(df)} rows and {len(df.columns)} columns.")
print("First 5 rows of the DataFrame:")
print(df.head())
except FileNotFoundError:
print(f"Error: The file '{file_path}' was not found. Check the path.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
Errors
Common errors & fixes
UnicodeDecodeError: 'latin-1' codec can't decode byte 0x...
The specified (or default) encoding for the SAS file is incorrect for the data it contains.
fixTry different encodings like `'cp1252'`, `'utf-8'`, or `'iso-8859-1'` when initializing `SAS7BDAT`. Example: `with SAS7BDAT('your_file.sas7bdat', encoding='cp1252') as reader:` FileNotFoundError: [Errno 2] No such file or directory: 'your_file.sas7bdat'
The `.sas7bdat` file path provided does not exist or is incorrect relative to the script's execution directory.
fixDouble-check the file path. Ensure it's absolute, or confirm the file is in the same directory as your script or a known relative path. Use `os.path.exists(file_path)` to debug.
AttributeError: 'SAS7BDAT' object has no attribute 'read_data'
You are attempting to use the `read_data()` method from `sas7bdat` versions prior to 2.0.0, which has been replaced.
fixUpdate your code to use the `to_data_frame()` method, which returns a pandas DataFrame. Example: `df = reader.to_data_frame()`.
sas7bdat.sas7bdat.SAS7BDATError: The file 'your_file.txt' is not a sas7bdat file.
The file provided to `SAS7BDAT` is either corrupted, not a valid `.sas7bdat` file, or has an incorrect extension.
fixEnsure the file is genuinely a `.sas7bdat` file. Verify its integrity and correct file extension. The library cannot parse arbitrary binary files.
Upgrade
Version history
2.2.3latest on PyPI · released Jul 15, 2019
Audit
Dependencies
pandasrequiredEssential for converting SAS data to DataFrames, which is the primary output format.