Press "Enter" to skip to content

How to Read SAS Files in Python with Pandas

Last updated on November 16, 2019

Share if You Like!

In this post, we are going to learn how to read SAS (.sas7bdat) files in Python.

As previously described (in the read .sav files in Python post) Python is a general-purpose language that also can be used for doing data analysis and data visualization.

One potential downside, however, is that Python is not really user-friendly for data storage. This has, of course, lead to that our data many times are stored using Excel, SPSS, SAS, or similar software. See, for instance, the posts about reading .sav, .dta, and .xlxs files in Python:

Can I Open a SAS File in Python?

Now we may want to answer the question of how to open a SAS file in Python? In Python, there are two useful packages Pyreadstat, and Pandas that enable us to open SAS files. If we are working with Pandas, the  read_sas method will load a .sav file into a Pandas dataframe. Note, Pyreadstat which is dependent on Pandas, will also create a Pandas dataframe from a .sas file.

How to Open a SAS file in Python

In this section, we are going to learn how to load a SAS file in Python using the Python package Pyreadstat. Of course, before we use Pyreadstat we need to make sure we have it installed.

How to install Pyreadstat:

Pyreadstat can be installed either using pip or conda:

  1. Install Pyreadstat using pip:
    Open up a terminal, or Windows PowerShell, and type pip install pyreadstat
  2. Install using Conda:
    Open up a terminal, or Windows PowerShell, and type conda install -c conda-forge pyreadstat

How to Load a .sas7bdat File in Python Using Pyreadstat

In this section, we are going to use pyreadstat to import data into a Pandas dataframe. First, we import pyreadstat:

import pyreadstat

Now, we are ready to import SAS files using the method read_sas7bdat (download airline.sas7bdat). Note that, when we load a file using the Pyreadstat package, recognize that it will look for the file in Python’s working directory.

df, meta = pyreadstat.read_sas7bdat('airline.sas7bdat')

In the code chunk above we create two variables; df, and meta. As can be seen when using type the variable “df” is a Pandas dataframe:

type(df)

Thus, we can use all methods available for Pandas dataframe objects. In the next line of code, we are going to print the 5 first rows of the dataframe using pandas head method.

df.head()

See more about working with Pandas dataframes in the following tutorials:

How to Read a SAS file with Python Using Pandas

In this section, we are going to load the same .sav7bdat file into a Pandas dataframe but by using Pandas read_sas method, instead. This has the advantage that we can load the SAS file from a URL.

Before we continue, we need to import Pandas:

import pandas as pd

Now, when we have done that, we can read the .sas7bdat file into a Pandas dataframe using the read_sas method. In the read SAS example here, we are importing the same data file as in the previous example.

Here, we print the 5 last rows of the dataframe using Pandas tail method.

url = 'http://www.principlesofeconometrics.com/sas/airline.sas7bdat'

df = pd.read_sas(url)
df.tail()

How to Read a SAS File and Specific Columns

Note, that read_sas7bdat (Pyreadstat) have the argument “usecols”. By using this argument, we can also select which columns we want to load from the SAS file to the dataframe:

cols = ['YEAR', 'Y', 'W']
df, meta = pyreadstat.read_sas7bdat('airline.sas7bdat', usecols=cols)
df.head()

How to Save a SAS file to CSV

In this section of the Pandas SAS tutorial, we are going to export the .sas7bdat file to a .csv file. This is easily done, we just have to use the to_csv method from the dataframe object we created earlier:

df.to_csv('data_from_sas.csv', index=False)

Remember to put the right path, as the second argument, when using to_csv to save a .sas7bdat file as CSV.

Summary: Read SAS Files using Python

Now we have learned how to read and write SAS files in Python. It was quite simple and both methods are, in fact, using the same Python packages.

Share if You Like!

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: