Notes

sas2csv - A SAS to CSV Converter


uv run sas2csv ./data/sas ./data/csv

> Converting ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% | 0:00:00 | usa.sas7bdat > usa.csv.gz
> Converted 150/150 files without any errors.
> Original file size: 5.3 MB, converted file size: 1.6 MB (29%)

I understand that this is a very niche tool in that it is for people who have data in SAS format .sas7bdat, don’t want to use SAS, but still have a SAS licence. If you don’t want to use SAS at all, there are other tools to read and/or convert .sas7bdat files. However, since the .sas7bdat file format is proprietary and undocumented, available solutions are slow and fragile, especially for larger SAS files.

So, if you are finding yourself in a situation like me, where you have to deal with data locked in .sas7bdat files and you have a SAS licence (for some reason), but you would much rather just work with .csv files using another language (e.g. R, Python, Julia), this tool is for you!

A bonus is that compressed .csv.gz files are often significantly smaller in size, compared with the .sas7bdat equivalents.

You can find the Git repository here: https://gitlab.com/alping/sas2csv

Prerequisites:

  • SAS (make sure SAS is on the path)
  • Python

CLI

uv tool add git+https://gitlab.com/alping/sas2csv.git
uv run sas2csv

Python Package

uv add git+https://gitlab.com/alping/sas2csv.git
from sas2csv import sas2csv, sas_in_path

sas_in_path()
sas2csv("path/to/sas_file.sas7bdat", "path/to/csv_file.csv.gz")

SAS Script

If you want to use the SAS script by itself, download the script file (sas2csv.sas) from the repository and call it with SAS:

sas sas2csv.sas ^
  -set IN "<absolute_path>/<filename>.sas7bdat" ^
  -set OUT "<absolute_path>/<filename>.csv.gz" ^
  -set GZ 1