Ready to get started?

Download a free trial of the SAS Data Sets Cmdlets to get started:

 Download Now

Learn more:

SAS Data Sets Icon SAS Data Sets Data Cmdlets

An easy-to-use set of PowerShell Cmdlets offering real-time access to SAS Data Sets. The Cmdlets allow users to easily read, write, update, and delete live data - just like working with SQL server.

PowerShell Scripting to Replicate SAS Data Sets Data to MySQL

Write a simple PowerShell script to replicate SAS Data Sets data to a MySQL database.

The CData Cmdlets for SAS Data Sets offer live access to SAS Data Sets data from within PowerShell. Using PowerShell scripts, you can easily automate regular tasks like data replication. This article will walk through using the CData Cmdlets for SAS Data Sets and the CData Cmdlets for MySQL in PowerShell to replicate SAS Data Sets data to a MySQL database.

After obtaining the needed connection properties, accessing SAS Data Sets data in PowerShell and preparing for replication consists of four basic steps.

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.

Collecting SAS Data Sets Data

  1. Install the module:

    Install-Module SASDataSetsCmdlets
  2. Connect to SAS Data Sets:

    $sasdatasets = Connect-SASDataSets -URI $URI
  3. Retrieve the data from a specific resource:

    $data = Select-SASDataSets -Connection $sasdatasets -Table "restaurants"

    You can also use the Invoke-SASDataSets cmdlet to execute pure SQL-92 statements:

    $data = Invoke-SASDataSets -Connection $sasdatasets -Query 'SELECT * FROM restaurants WHERE cuisine = @cuisine' -Params @{'@cuisine'='American'}
  4. Save a list of the column names from the returned data.

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name

Inserting SAS Data Sets Data into the MySQL Database

With the data and column names collected, you are ready to replicate the data into a MySQL database.

  1. Install the module:

    Install-Module MySQLCmdlets
  2. Connect to MySQL, using the server address and port of the MySQL server, valid user credentials, and a specific database with the table in which the data will be replicated:

    $mysql = Connect-MySQL -User $User -Password $Password -Database $Database -Server $Server -Port $Port
  3. Loop through the SAS Data Sets data, store the values, and use the Add-MySQL cmdlet to insert the data into the MySQL database, one row at a time. In this example, the table will need to have the same name as the SAS Data Sets resource (restaurants) and to exist in the database.

    $data | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "restaurants" -Columns $columns -Values $values }

You have now replicated your SAS Data Sets data to a MySQL database. This gives you freedom to work with SAS Data Sets data in the same way that you work with other MySQL tables, whether that is performing analytics, building reports, or other business functions.


  • Once you have connected to SAS Data Sets and MySQL in PowerShell, you can pipe command results to perform the replication in a single line:

    Select-SASDataSets -Connection $sasdatasets -Table "restaurants" | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "restaurants" -Columns $columns -Values $values }
  • If you wish to replicate the SAS Data Sets data to another database using another PowerShell module, you will want to exclude the Columns, Connection, and Table columns from the data returned by the Select-SASDataSets cmdlet since those columns are used to help pipe data from one CData cmdlet to another:

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name | ? {$_ -NotIn @('Columns','Connection','Table')}