We are proud to share our inclusion in the 2024 Gartner Magic Quadrant for Data Integration Tools. We believe this recognition reflects the differentiated business outcomes CData delivers to our customers.
Get the Report →How to Build an ETL App for SAS Data Sets Data in Python with CData
Create ETL applications and real-time data 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 DataSet files:
Connecting to Local Files
- Set the Connection Type to "Local." Local files support SELECT, INSERT, and DELETE commands.
- Set the URI to a folder containing SAS files, e.g. C:\PATH\TO\FOLDER\.
Connecting to Cloud-Hosted SAS DataSet Files
While the driver is capable of pulling data from SAS DataSet files hosted on a variety of cloud data stores, INSERT, UPDATE, and DELETE are not supported outside of local files in this driver.
Set the Connection Type to the service hosting your SAS DataSet files. A unique prefix at the beginning of the URI connection property is used to identify the cloud data store and the remainder of the path is a relative path to the desired folder (one table per file) or single file (a single table). For more information, refer to the Getting Started section of the Help documentation.
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 CData Python Connector for SAS Data Sets 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')