![]() |
VOOZH | about |
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 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:
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.
Use the pip utility to install the required modules and frameworks:
pip install petl pip install pandas
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;")
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'"
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.
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.
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.
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.
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')
Download a Community License of the SAS Data Sets Connector to get started:
Download NowLearn more:
👁 SAS Data Sets IconPython Connector Libraries for SAS Data Sets Data Connectivity. Integrate SAS Data Sets with popular Python tools like Pandas, SQLAlchemy, Dash & petl.