Skip to content

Instantly share code, notes, and snippets.

@cjdinger
Last active November 10, 2022 19:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cjdinger/4771455 to your computer and use it in GitHub Desktop.
Save cjdinger/4771455 to your computer and use it in GitHub Desktop.
Example of how to use PowerShell to script the SAS Integration Technologies client. You can connect to a remote SAS Workspace and run a program, retrieve the SAS log and listing.
# Example of how to use PowerShell to script the
# SAS Integration Technologies client
# You can connect to a remote SAS Workspace
# and run a program, retrieve the SAS log and listing
# To use: change this script to reference your SAS Workspace
# node name, port (if different), and user credentials
# create the Integration Technologies objects
$objFactory = New-Object -ComObject SASObjectManager.ObjectFactoryMulti2
$objServerDef = New-Object -ComObject SASObjectManager.ServerDef
$objServerDef.MachineDNSName = "sasnode.mycompany.com" # SAS Workspace node
$objServerDef.Port = 8591 # workspace server port
$objServerDef.Protocol = 2 # 2 = IOM protocol
# Class Identifier for SAS Workspace
$objServerDef.ClassIdentifier = "440196d4-90f0-11d0-9f41-00a024bb830c"
# create and connect to the SAS session
$objSAS = $objFactory.CreateObjectByServer(
"SASApp", # server name
$true,
$objServerDef, # built server definition
"sasdemo", # user ID
"secretPassword" # password
)
# program to run
# could be read from external file
$program = "options formchar='|----|+|---+=|-/\<>*';"
$program += "ods listing; proc means data=sashelp.cars mean mode min max; run;"
# run the program
$objSAS.LanguageService.Submit($program);
# flush the output - could redirect to external file
Write-Output "Output:"
$list = ""
do
{
$list = $objSAS.LanguageService.FlushList(1000)
Write-Output $list
} while ($list.Length -gt 0)
# flush the log - could redirect to external file
Write-Output "LOG:"
$log = ""
do
{
$log = $objSAS.LanguageService.FlushLog(1000)
Write-Output $log
} while ($log.Length -gt 0)
# end the SAS session
$objSAS.Close()
@sangramjitpanda
Copy link

how to know the exit code of the sas program if we ant to cpature?

@cjdinger
Copy link
Author

You could try FlushLogLines -- which includes attributes for each line of the log (ERROR, NOTE, WARNING, etc). Example in C# is here: https://github.com/cjdinger/SasHarness/blob/master/Mainform.cs

@sangramjitpanda
Copy link

Actually I am using the above script but instead of running the SAS statements directly from script, I call multiple .sas file like below:

$program1 += Get-Content "\XXX\fs\XX\userhome\5\XXX\Desktop\EG_TEST_DEV.sas"
$program2 += Get-Content "\XXX\fs\XX\userhome\5\XXX\Desktop\EG_TEST_DEV2.sas"

We are able to run the programs but challenge here is we want to run second program based on successful execution of 1st program. So, trying to get the exit code for first .sas program and based on that it would call second 2nd sas program. Is it possible to capture the exit code? Just wanted to inform that Workspace/Compute tier is on Linux platform. We are invoking the PowerShell script where .sas program resides on Windows Path.

@cjdinger
Copy link
Author

The most reliable way that I know of is to scan the log (returned in FlushLog or FlushLogLines) for ERROR or WARNING lines, based on your needs. Then use that to determine whether the first program was "successful".

@sangramjitpanda
Copy link

Ok. Thanks Chris! Will give a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment