Extract, Transform, and Load SAS Data Sets Data in Python

Ready to get started?

Download for a free trial:

Download Now

Learn more:

SAS Data Sets Python Connector

Python Connector Libraries for SAS Data Sets Data Connectivity. Integrate SAS Data Sets with popular Python tools like Pandas, SQLAlchemy, Dash & petl.



The CData Python Connector for SAS Data Sets enables you to create ETL applications and pipelines for SAS Data Sets data in Python with petl.

The rich ecosystem of Python modules lets you get to work quickly and integrate your systems more effectively. With the CData Python Connector for SAS Data Sets and the petl framework, you can build SAS Data Sets-connected applications and pipelines for extracting, transforming, and loading SAS Data Sets data. This article shows how to connect to SAS Data Sets with the CData Python Connector and use petl and pandas to extract, transform, and load SAS Data Sets data.

With built-in, optimized data processing, the CData Python Connector offers unmatched performance for interacting with live SAS Data Sets data in Python. When you issue complex SQL queries from SAS Data Sets, the driver pushes supported SQL operations, like filters and aggregations, directly to SAS Data Sets and utilizes the embedded SQL engine to process unsupported operations client-side (often SQL functions and JOIN operations).

Connecting to SAS Data Sets Data

Connecting to SAS Data Sets data looks just like connecting to any relational data source. Create a connection string using the required connection properties. For this article, you will pass the connection string as a parameter to the create_engine function.

Set the following connection properties to connect to your SAS DataSets files:

  • URI: Set this to the folder containing your .sas7bdat resources. Currently we only support local files.

After installing the CData SAS Data Sets Connector, follow the procedure below to install the other required modules and start accessing SAS Data Sets through Python objects.

Install Required Modules

Use the pip utility to install the required modules and frameworks:

pip install petl
pip install pandas

Build an ETL App for SAS Data Sets Data in Python

Once the required modules and frameworks are installed, we are ready to build our ETL app. Code snippets follow, but the full source code is available at the end of the article.

First, be sure to import the modules (including the CData Connector) with the following:

import petl as etl
import pandas as pd
import cdata.sasdatasets as mod

You can now connect with a connection string. Use the connect function for the CData SAS Data Sets Connector to create a connection for working with SAS Data Sets data.

cnxn = mod.connect("URI=C:/myfolder;")

Create a SQL Statement to Query SAS Data Sets

Use SQL to create a statement for querying SAS Data Sets. In this article, we read data from the restaurants entity.

sql = "SELECT name, borough FROM restaurants WHERE cuisine = 'American'"

Extract, Transform, and Load the SAS Data Sets Data

With the query results stored in a DataFrame, we can use petl to extract, transform, and load the SAS Data Sets data. In this example, we extract SAS Data Sets data, sort the data by the borough column, and load the data into a CSV file.

Loading SAS Data Sets Data into a CSV File

table1 = etl.fromdb(cnxn,sql)

table2 = etl.sort(table1,'borough')

etl.tocsv(table2,'restaurants_data.csv')

In the following example, we add new rows to the restaurants table.

Adding New Rows to SAS Data Sets

table1 = [ ['name','borough'], ['Newname1','Newborough1'], ['Newname2','Newborough2'], ['Newname3','Newborough3'] ]

etl.appenddb(table1, cnxn, 'restaurants')

With the CData Python Connector for SAS Data Sets, you can work with SAS Data Sets data just like you would with any database, including direct access to data in ETL packages like petl.

Free Trial & More Information

Download a free, 30-day trial of the SAS Data Sets Python Connector to start building Python apps and scripts with connectivity to SAS Data Sets data. Reach out to our Support Team if you have any questions.



Full Source Code


import petl as etl
import pandas as pd
import cdata.sasdatasets as mod

cnxn = mod.connect("URI=C:/myfolder;")

sql = "SELECT name, borough FROM restaurants WHERE cuisine = 'American'"

table1 = etl.fromdb(cnxn,sql)

table2 = etl.sort(table1,'borough')

etl.tocsv(table2,'restaurants_data.csv')

table3 = [ ['name','borough'], ['Newname1','Newborough1'], ['Newname2','Newborough2'], ['Newname3','Newborough3'] ]

etl.appenddb(table3, cnxn, 'restaurants')