Skip to content
Advertisement

Convert SAS data to a python dataframe

I have this small little code here to import a SAS file into dataframe in Python.

from sas7bdat import SAS7BDAT
with SAS7BDAT('some_file.sas7bdat') as f:
    df = f.to_data_frame()
print df.head(5)

The code runs forever without any output. The sas file I’m trying to import is 1.5gb.

Advertisement

Answer

You should use the native pandas function pandas.read_sas it’s faster than iterating through the file as you did.

Here is the documentation of the pandas.read_sas function. This code sample should be sufficient to load the file:

df = pandas.read_sas('some_file.sas7bdat')
print(df.head())
Advertisement