Using SAS Enterprise Guide to run programs in batch

SAS Enterprise Guide is best known as an interactive interface to SAS, but did you know that you can use it to run batch-style programs as well?

SAS Enterprise Guide has always offered an automation object model, which allows you to use scripting languages (such as VBScript or Windows PowerShell) to call the application's features from within other processes. (See Microsoft's "scripting center" for quick access to All Things Scripting for your Windows PC.)

It's this automation model that allows you to schedule a project or process flow to run unattended. When you select File->Schedule Project, SAS Enterprise Guide creates a default script file (using VBScript) and adds a scheduled task to the Windows scheduler. That's convenient, but you don't have to use the Schedule feature to take advantage of automation. You can write your own scripts to automatically run tasks, projects, or SAS programs at any time.

The automation model even allows you to create a new project on-the-fly, and add SAS programs into the project and run them. This way, you don't need to create a predefined SAS Enterprise Guide project (EGP file) in order to run SAS programs in your environment. The automation model uses the SAS Enterprise Guide application to provide the "plumbing" to access your SAS metadata environment and SAS workspace servers and run any SAS job...even when you don't have SAS on your local PC.

Note for Windows x64 users: SAS Enterprise Guide is a 32-bit application. In order to run scripts properly on a 64-bit operating system, be sure to use the 32-bit version of whatever scripting runtime is needed. For example, to run a VBScript file, use a command such as:

c:\Windows\SysWOW64\cscript.exe c:\projects\AutomationNewProgram.vbs

You can learn more about the automation API by perusing the reference documentation (the link is for the 4.2 version, but the 4.3 version is virtually unchanged). You can also learn from the examples provided within this SAS sample.

UPDATE 10Nov2012: Since this original post, I've written a conference paper and several more examples. See the details about the paper and examples at Doing more with SAS Enterprise Guide automation.

I've included an additional useful sample here. This VBScript program allows you to connect to your SAS workspace server and run any program code that you want, and then save the output log and listing to your local PC file system. To use the example, save it to a .VBS file on the machine where SAS Enterprise Guide is installed. Of course you'll need to change the names of the active profile and SAS server to suit your environment. Then you can run the example using the proper version of cscript.exe.

Option Explicit ' Forces us to declare all variables

Dim app ' application
Dim project ' Project object
Dim sasProgram ' Code object (SAS program)
Dim n ' counter

' Use SASEGObjectModel.Application.4.2 for EG 4.2
Set app = CreateObject("SASEGObjectModel.Application.4.3")
' Set to your metadata profile name, or "Null Provider" for just Local server
app.SetActiveProfile("My Server")
' Create a new project
Set project = app.New
' add a new code object to the project
Set sasProgram = project.CodeCollection.Add

' set the results types, overriding app defaults
sasProgram.UseApplicationOptions = False
sasProgram.GenListing = True
sasProgram.GenSasReport = False

' Set the server (by Name) and text for the code
sasProgram.Server = "SASApp"
sasProgram.Text = "proc means data=sashelp.cars; run;"

' Run the code
sasProgram.Run
' Save the log file to LOCAL disk
sasProgram.Log.SaveAs "c:\temp\outputAuto.log"

' Filter through the results and save just the LISTING type
For n=0 to (sasProgram.Results.Count -1)
' Listing type is 7
If sasProgram.Results.Item(n).Type = 7 Then
' Save the listing file to LOCAL disk
sasProgram.Results.Item(n).SaveAs "c:\temp\outputAuto.lst"
End If
Next
app.Quit