Menu

SAS automation

Andreas Menrath
Attachments
Automation_sample.png (103770 bytes)

Sample: SAS automation

Overview

You can use PowerShell Variables with the Cmdlets4Sas.
This enables you to automate your workflow. All the control logic is handled in powershell.

In this sample we will cover the following steps:

  • Start a local SAS Workspace Server
  • Submit SAS code
  • Use PowerShell variables in SAS code
  • Read the SAS log
  • Close the Workspaceserver session

Code

# ensure module is loaded
if(!(Get-Module Cmdlets4Sas)) 
    {Import-Module Cmdlets4Sas}

# Connect to server
$ws = Connect-SasWorkspaceLocal

# define macro
$code = @"
%macro hello(name);
  %put Hello &name!;
%mend hello;
"@

# submit code
Invoke-SasCode $ws $code

# create an powershell array with some data
# you may also import data from e.g. a local CSV file with the Import-Csv Cmdlet
$names = @("alice",
           "alfred",
           "barbara",
           "bob",
           "carol",
           "$env:USERNAME"   # use powershell variable with your username
           )

# submit SAS code for each item in the array
foreach ($name in $names)
{
  if ($name -ne "bob")   # check if name is not equal to "bob"
  {
    # use powershell variable $name in SAS code
    Invoke-SasCode $ws "%hello($name);"
  }
}

# write the SAS log to the console
Read-SasLog $ws

Disconnect-SasServer $ws

Results


Related

Wiki: Samples

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.