BookmarkSubscribeRSS Feed
stf5018
SAS Employee
Are you a SAS practitioner who constantly gets complaints from your Python pals about using your data? Are you a Python guru who inherited some data files in something called “sas7bdat”? What’s dat, you ask? Well, this post is for you!

sas7bdat is nothing more than a native data storage format used by SAS and there are actually several ways you can read sas7bdat files in Python. I’ll describe 2 of the more common ones here.

Unless you’re reading this and still think Python is a snake, you probably already guessed the first method.

Using Pandas

Yep, Pandas to the rescue yet again. All you need is 2 lines of code:
import pandas as pd
df_sas = pd.read_sas('location/filename.sas7bdat')

 

…aaaaand that’s it! Unfortunately you can’t go the other direction with Pandas, but have no fear. There’s another library for that.

Using saspy

The second way is to use SAS’ saspy Python library. This is an open source library just like any other, although it does require you to have a SAS license and be configured properly. Luckily with Workbench, this is installed, configured, and ready to go without any additional work on your part! The code itself is simple and takes just a few more lines of code than the Pandas method, and it still imports the data into our trusty Pandas DataFrame format.
import saspy
sas = saspy.SASsession()
sas.saslib('mylib',path='/path_to_data_folder')
df_sas = sas.sd2df(table='tablename', libref='mylib')

 

The beauty of Workbench is that if you already have librefs defined, such as those predefined in an autoexec, you have access to those too within the Workbench environment letting you seamlessly work in both SAS and Python.

And of course, saspy lets you convert your Pandas DataFrame back to sas7bdat.
sas.dataframe2sasdata(df='df_sas', table='tablename', libref='mylib')

 

And since I know you SAS programmers out there are pretty sharp - you now understand that all the kids on your team were not, in fact, talking about cute and cuddly bears, and the above line of code is how you can now use their Pandas data and move them into SAS.

Happy data wrangling!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Discussion stats
  • 0 replies
  • 702 views
  • 6 likes
  • 1 in conversation