Wednesday, 31 August 2016

Invoking Stored Processes with Powershell - the easy way

There are a number of ways to run SAS from Powershell:
  1. Using SAS Integration Technologies (Chris Hemedinger has some great resources on this)
  2. Directly by calling sas.exe (presuming you are running on a box with a local install)
  3. Indirectly via the SAS Stored Process Web Application.
Let me explain the awesomeness of option 3:
  • It deals with authentication (no need for embedded passwords)
  • Can perform tasks using system account (sassrv), as SAS code is secured server-side
  • Resource management (no need to tidy up any redundant sessions)
  • Can be done in just ONE LINE of Powershell code!
For the sake of mobile readers, I'm going to split the example over three lines:
$pgm="/Products/SAS Intelligence Platform/Samples/Sample: Hello World"
invoke-webrequest -Uri "$($uri)?_program=$pgm" -UseDefaultCredentials
Which gives the response:

The key part to this request, to avoid HTTP Status 401 and "This request requires HTTP authentication" response, is the -UseDefaultCredentials parameter.  This uses the account that Powershell is running in, to connect to the server.

Some prerequisites:
  • You need to configure your SAS code to run inside a Stored Process using SAS Management Console - set for streaming output 
  • You may need to set your execution policy (explained here)
  • The account being used to run the script must have a SAS identity, and the web server must be configured for single signon.

Tuesday, 9 August 2016

Chaining Windows Commands in SAS Filename Pipe (&)

Using filename pipe (instead of the X command, or call execute) is a great way to submit OS commands, not least because the standard output can be read directly into SAS.

But how does one submit multiple commands in the same filename (or file) statement?

Easy - & I'll show you how:

  data _null_;
    infile "D: & cd ""%sysfunc(pathname(work))"" & dir" pipe;

The above snippet opens a shell (via the pipe fileref), changes the working directory to the D drive, then to the work folder (our work location is on a different drive), then executed the dir command to demonstrate that we are indeed in the work folder.

The delimiter is, of course, the ampersand (&).