Registry / data / sas7bdat

sas7bdat

JSON →
library2.2.3pypypi✓ verified 84d ago

The `sas7bdat` library provides a Pythonic way to read SAS `.sas7bdat` files, making it easy to convert them into pandas DataFrames. As of version 2.2.3, it offers robust parsing capabilities for various SAS file versions and handles common encoding challenges. The library generally releases updates for bug fixes and minor feature enhancements.

pip install sas7bdat
INSTALL
IMPORT
SIG · SAS7BDAT
S
sas7bdat
datapythonv2.2.3
Install
2.5s avg
Import
40ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
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
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.044s · 19.3MB
glibc
py 3.103.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}")
Debug
Known issues
breakingThe `read_data()` method was significantly changed in version 2.0.0. It no longer returns a list of tuples representing data rows. Instead, `to_data_frame()` should be used to get a pandas DataFrame.
fix
Replace calls to `reader.read_data()` with `reader.to_data_frame()`. Ensure you have pandas installed.
affects: >=2.0.0
gotchaSAS files often use various encodings (e.g., `latin-1`, `cp1252`, `utf-8`). If not specified correctly, `UnicodeDecodeError` or incorrect characters will appear. The library defaults to `latin-1`.
fix
Always pass the `encoding` parameter to the `SAS7BDAT` constructor, e.g., `SAS7BDAT('file.sas7bdat', encoding='cp1252')`. Refer to your SAS environment or try common encodings.
affects: All
gotchaReading very large SAS files into a pandas DataFrame can consume significant memory, potentially leading to `MemoryError`.
fix
For very large files, consider iterating over the data in chunks using the `chunksize` parameter (e.g., `SAS7BDAT(..., chunksize=10000)`). Process each chunk separately or load only necessary columns.
affects: All
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.
fix
Try 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.
fix
Double-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.
fix
Update 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.
fix
Ensure 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.
Agent activity
12 hits · last 30 days
node
10
OpenAI (training)
1
Resources
sas7bdat — pip install sas7bdat · libregistry