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.

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
tags: automation, SAS Enterprise Guide, SAS tips, scripting

17 Comments

  1. John Hendrickx
    Posted June 10, 2011 at 7:35 am | Permalink

    Very interesting example, I know several people who want to do something like this. It works in part for me. The SAS log is generated but no other output. If I add a line "msgbox project.Results.Count" then the message box reports a 0. Any idea how to fix this? I'm using SAS 9.2, EG 4.3 with a remote server.

    Best regards,
    John Hendrickx

  2. Wendy
    Posted June 14, 2011 at 5:06 pm | Permalink

    Hi Chris,

    Great post! Any chance you can post the link differently to the automation API documentation? My laptop is not able to grab the file for some reason.I would be interested in reading it.

    Thank you!

    Wendy

  3. Chris
    Posted June 15, 2011 at 4:32 pm | Permalink

    Hi Wendy, you might be running into the issue with CHM file content being "locked" when you download it from the web? If you can get the ZIP file and extract the CHM file, right-click on the CHM file and select Properties, then Security, and "unblock" it so that you can read it.

    If that's not the issue, post back.

  4. jn
    Posted September 27, 2011 at 9:02 am | Permalink

    thanks for sharing. i am stuck on submitting a specific program (or Code Object, which falls under a CodeCollection). it is also of importance that the code object is NOT in the project. basically, i would like to run a stand-alone .sas program that is not in any project (.egp) file.

    my gameplan is to write code to accomplish the following:
    1. open a new SAS EG app
    2. add a new project.
    3. open/add a .sas program from the filesystem 4. run that program 5. close the program and project without saving either

    i would ideally have liked to do the following, but i THINK i need to have a project first:
    1. open a new SAS EG app
    2. open/add a .sas program from the filesystem 3. run that program 4. close app

    also, if you know of documentation that is less terse (and includes more examples and better description of how to use these objects in conjunction with each other), that would be very helpful.

    thanks in advance

    • Chris Hemedinger Chris Hemedinger
      Posted September 27, 2011 at 9:06 am | Permalink

      You can accomplish this by adapting the technique in the example. But instead of having the SAS program in the script code, use the VBScript File object model to open and read the contents of the SAS program that you want to use (from your local PC) and set that to be the Text value of the Code object.

      There are some examples of using VBScript to read text files here.

  5. Loic
    Posted November 2, 2011 at 1:00 pm | Permalink

    Awesome job !

    Any chance to get this code translate in powershell ?

    beg of you :)

  6. Tim
    Posted December 9, 2011 at 1:20 pm | Permalink

    Hi Chris,

    Is there a way to pass macro variables to the SAS program?

    Thanks,
    Tim

    • Chris Hemedinger Chris Hemedinger
      Posted December 9, 2011 at 1:27 pm | Permalink

      Hi Tim,
      The easiest method would be to precede the SAS program in the VB Script with a series of %let statements. That works if you don't mind "hard-coding" the macro variables into the script.

      If you want your script to be resusable in multiple scenarios, and dynamically set macro variables, you can modify the script to accept command-line arguments. Then you can use those argument values in your scripting code to build the %LET statements at the front of your SAS program.

      You can find more information about using command-line arguments with VB Script here, or more details here.

  7. Xavier
    Posted January 9, 2012 at 6:28 am | Permalink

    It is a good thing that we have this functionality in EGuide but I can see many pitfalls for the SAS administrator.

    The users will use their PC to automate task, it is preferable to use the server and dedicated tools to do that job (cron, control-M ...)
    There is a risk of overload in the server : each time the users will launch a VBS a connection will be made and a process will be launched. Imagine that you have 10 users launching 20 VBS each day.

    So administrators think twice if you want to offer this task to your users.

  8. John Hendrickx
    Posted January 10, 2012 at 9:21 am | Permalink

    Hi Chris,

    I've got it up and running here too, I think it's great! But I was wondering if you know of a way to kill a batch job (under Windows)?

    • Chris Hemedinger Chris Hemedinger
      Posted January 10, 2012 at 9:28 am | Permalink

      John,

      Glad to hear it's working well for you.

      I'm a big fan of the Sysinternals tools, now available for download from Microsoft. PSKILL is a very useful command that can kill processes on your local and remote Windows boxes; you can download it here.

  9. PV
    Posted April 1, 2012 at 2:09 am | Permalink

    Hi,

    Assigning a library missing in above code. Please explain me how to assign any library using macro.

    • Chris Hemedinger Chris Hemedinger
      Posted April 1, 2012 at 8:57 am | Permalink

      PV, within this example you could include a LIBNAME statement in the Text property of the SAS code that is submitted. I plan to provide additional examples of automating SAS Enterprise Guide using script very soon. It's a topic that I'm preparing for SAS Global Forum 2012.

  10. tim walters
    Posted April 19, 2012 at 10:26 am | Permalink

    I embedded this into a Microsoft Excel VBA application and it works for users with 32-bit machines. I have a user with a 64-bit machine who can't get past this statement.

    Set app = CreateObject("SASEGObjectModel.Application.4.3")

    What do I have to do to get it to work for him?

    • Chris Hemedinger Chris Hemedinger
      Posted April 19, 2012 at 10:32 am | Permalink

      Tim, if the user has 64-bit MS Office, this won't work. The 64-bit Office process cannot invoke the 32-bit EG modules.

      It's not 64-bit MS Windows - that can work fine using the 32-bit subsystem on the OS. It's the fact that MS Office is a 64-bit process.

      How to make it work:

      Replace 64-bit MS Office with 32-bit MS Office. Do you need the 64-bit version of Office? Most people do not.

      Use EG 5.1, which offers a 64-bit version.

4 Trackbacks

  1. By Running Windows PowerShell Scripts - The SAS Dummy on September 12, 2011 at 8:07 am

    [...] have used Windows PowerShell to automate some of my SAS-related processes, such as batch processing with SAS Enterprise Guide.  I've also used it within my development work to gather metrics about files, computers on the [...]

  2. [...] SAS Enterprise Guide supports a scriptable object model, which allows you to write scripts or programs to automate many aspects of the application without interacting with the application's user interface. [...]

  3. [...] You can also use the profile name in SAS Enterprise Guide automation scripts, as shown in this example about running SAS programs in batch. [...]

  4. [...] The scheduled task contains the command (CSCRIPT.EXE) and a reference to the VBScript (VBS file), which Windows will run at the appointed time. (Learn more about this mechanism at "Using SAS Enterprise Guide to run programs in batch".) [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <p> <pre lang="" line="" escaped=""> <q cite=""> <strike> <strong>