Get Started

Importing SAS files

In this exercise, you'll figure out how to import a SAS file as a DataFrame using SAS7BDAT and pandas. The file 'sales.sas7bdat' is already in your working directory and both pandas and matplotlib.pyplot have already been imported as follows:

import pandas as pd
import matplotlib.pyplot as plt

The data are adapted from the website of the undergraduate text book Principles of Econometrics by Hill, Griffiths and Lim.

This is a part of the course

“Introduction to Importing Data in Python”

View Course

Exercise instructions

  • Import the module SAS7BDAT from the library sas7bdat.
  • In the context of the file 'sales.sas7bdat', load its contents to a DataFrame df_sas, using the method .to_data_frame() on the object file.
  • Print the head of the DataFrame df_sas.
  • Execute your entire script to produce a histogram plot!

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import sas7bdat package
from ____ import ____

# Save file to a DataFrame: df_sas
with SAS7BDAT('sales.sas7bdat') as file:
    ____

# Print head of DataFrame


# Plot histogram of DataFrame features (pandas and pyplot already imported)
pd.DataFrame.hist(df_sas[['P']])
plt.ylabel('count')
plt.show()

This exercise is part of the course

Introduction to Importing Data in Python

BeginnerSkill Level
4.7+
88 reviews

Learn to import data into Python from various sources, such as Excel, SQL, SAS and right from the web.

You've learned how to import flat files, but there are many other file types you will potentially have to work with as a data scientist. In this chapter, you'll learn how to import data into Python from a wide array of important file types. These include pickled files, Excel spreadsheets, SAS and Stata files, HDF5 files, a file type for storing large quantities of numerical data, and MATLAB files.

Exercise 1: Introduction to other file typesExercise 2: Not so flat any moreExercise 3: Loading a pickled fileExercise 4: Listing sheets in Excel filesExercise 5: Importing sheets from Excel filesExercise 6: Customizing your spreadsheet importExercise 7: Importing SAS/Stata files using pandasExercise 8: How to import SAS7BDATExercise 9: Importing SAS files
Exercise 10: Using read_stata to import Stata filesExercise 11: Importing Stata filesExercise 12: Importing HDF5 filesExercise 13: Using File to import HDF5 filesExercise 14: Using h5py to import HDF5 filesExercise 15: Extracting data from your HDF5 fileExercise 16: Importing MATLAB filesExercise 17: Loading .mat filesExercise 18: The structure of .mat in Python

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free