Using Windows PowerShell to report on your SAS Enterprise Guide project files

This blog post is a "mashup" of a couple of my previous posts, combining the lessons to create something brand new that I hope you will find useful.  First, let's review what we know:

It's not a big leap to realize something that we scripting geeks will find very cool: we can use Windows PowerShell to control SAS Enterprise Guide.

In this example, we'll look at a script that can process one or more project files (.EGP files) and report on what's in them.  That's right: there is no need to open SAS Enterprise Guide to peek inside the project.  We can write a script that pulls the information out of the project for us.

Here is a copy of the script that you can download.  To follow along with the instructions in the rest of this post, you should download/copy the file and name it "eg43ProjectContents.ps1" (drop the TXT extension, which I used to make it safe for download).  The example is designed with work with SAS Enterprise Guide 4.3.

To run the script, you must invoke the powershell command or run the script from within a PowerShell console window.  (Remember, before you can run PowerShell scripts you must enable your Windows environment to allow it.)

IMPORTANT: Because SAS Enterprise Guide is a 32-bit application, you must run the 32-bit version of PowerShell with this script. See this post for more information about how to locate the 32-bit version even when running on 64-bit Windows.

Here is an example command that will report on a single EGP file:

powershell -command "ls c:\projects\DonorsChoose.egp | c:\Scripts\eg43ProjectContents.ps1"

To report on an entire directory of EGP files, simply change the command to:

powershell -command "ls c:\projects\*.egp | c:\Scripts\eg43ProjectContents.ps1"

Here is a partial example of the output for a particular project.  The output is emitted to the console, or STDOUT, but you can easily capture it in a file by redirecting the output.

Examining project: C:\Projects\DonorsChoose.egp
Process flows within project:
   Data Descriptors
     SASHELP VTABLE  ( SAS.EG.Scripting.Data )
     SASHELP VCOLUMN  ( SAS.EG.Scripting.Data )
     Data Description  ( SAS.EG.Scripting.Report )
     Data Imported from donorschoose-org-1may2011-v1-donations.csv  ( SAS.EG.Scripting.Data )
       Characterize Data  ( SAS.EG.Scripting.Task )
     Data Imported from donorschoose-org-1may2011-v1-projects.csv  ( SAS.EG.Scripting.Data )
       Characterize Data  ( SAS.EG.Scripting.Task )
     Data Imported from donorschoose-org-1may2011-v1-resources.csv  ( SAS.EG.Scripting.Data )
       Characterize Data  ( SAS.EG.Scripting.Task )
     Data Description  ( SAS.EG.Scripting.Query )
     Column descriptions  ( SAS.EG.Scripting.Query )
     Link to Data Imported from donorschoose-org-1may2011-v1-donations.csv  ( SAS.EG.Scripting.Link )
     Link to Data Imported from donorschoose-org-1may2011-v1-projects.csv  ( SAS.EG.Scripting.Link )
     Link to Data Imported from donorschoose-org-1may2011-v1-resources.csv  ( SAS.EG.Scripting.Link )
     Link to SASHELP VTABLE  ( SAS.EG.Scripting.Link )
     Link to SASHELP VCOLUMN  ( SAS.EG.Scripting.Link )
   About the data
     Normalized Data Schema  ( SAS.EG.Scripting.File )
     Data Schema  ( SAS.EG.Scripting.File )

You can view the full output for this example here.

The example script contains many code comments to help you to understand what's going on, but here are a few highlights. To "invoke" the SAS Enterprise Guide scripting object, you use PowerShell commands to create a COM object that represents the application:

# Launch the application using the registered prog ID
$eguideApp = New-Object -comObject SASEGObjectModel.Application.4.3 

To load a project into the application, use the Open() method (which accepts two arguments: a file name and optionally, a password -- for protected project files). Because this script accepts a list of files piped in from the command invocation, the candidate files are stored in the special $input variable:

foreach ($projName in $input)
{
 # Open the project file
 Write-Host " --------------------------------------------- "
 Write-Host "Examining project:" $projName
 $project = $eguideApp.Open("$projName", "")

 # all other processing for this project
 # ....
 # Close the project file
 $project.Close()
}

To iterate through each process flow within a project, use the ContainerCollection property on the Project object. Note that there are different types of top-level "containers" in the object model. The Process Flow is the most common, but there are also containers for Reports, Distribution (such as Send E-mail steps) and Ordered Lists. The Process Flow has a ContainerType of 0.

# Show all of the process flows in the project
$pfCollection = $project.ContainerCollection
Write-Host "Process flows within project:"
foreach ($pf in $pfCollection)
{
  if ($pf.ContainerType -eq 0)
  {
    Write-Host "  " $pf.Name
    # To RUN a process flow, simply use $pf.Run()
  }
}

Note: As you review the output from the scripting model, you might notice that the objects are not reported in the same sequence that you expect to find them visually when you view the project in SAS Enterprise Guide. For example, the Process Flow objects are not always reported in the same order that you see them in the project tree. Alas, the SAS Enterprise Guide automation model does not offer a completely accurate representation of the object arrangement in your project and process flows, only the content.

To write your own scripts, you will need the reference documentation for the SAS Enterprise Guide object model. That's available here, in a CHM help file contained within a ZIP file. (Note that this is the version 4.2 documentation, but the object model is the same in 4.3. To view the contents of the CHM file, you may need to "unblock" it within your Windows environment.)

tags: automation, batch processing, PowerShell, SAS Enterprise Guide, scripting

6 Comments

  1. peterC
    Posted November 10, 2011 at 6:47 am | Permalink

    very interesting!
    can we automate this within SAS to, say, report whether any (and which) objects use a particular column name within a project?

    • Chris Hemedinger Chris Hemedinger
      Posted November 10, 2011 at 9:41 am | Permalink

      Peter, good question!

      I plan to have one or two more posts on this topic, showing how to dive deeper in the project contents. From there, creative problem solvers like yourself can probably figure something out.

  2. Ramana Potluri
    Posted November 24, 2011 at 4:39 pm | Permalink

    Hi ,
    I have about SAS EG projects which I have to script them into *.sas Files.
    Can you please tell me if there is easy way to do this. I have to repeat this every week.
    Your help inthis regard is highly appreciated.
    Thanks

    • Chris Hemedinger Chris Hemedinger
      Posted November 28, 2011 at 2:32 pm | Permalink

      Unfortunately, there is not a way to automate the "Export All Code" feature in scripting language using SAS Enterprise Guide automation.

      It is possible to use the scripting language to enumerate all of the task and code items within a project, and then export each one individually (using the SaveAs method for each one). But that does not take into account the sequence of items as shown in the process flow, so it's possible (likely) that the items would be exported out of order.

      It would also be possible to create a custom task and use the ISASProject interfaces to automate some of this. There is an example that uses the ISASProject interface here: http://support.sas.com/documentation/onlinedoc/guide/customtasks/. See the "Program Manager" example. Creating a custom task requires .NET programming.

  3. Antonie Victor
    Posted May 30, 2013 at 5:40 am | Permalink

    Hi,
    I don't have much experience with Powershell scripting, however I want to create a script that can use the functionality of formatting script in EG "Ctrl I" is the shortcut. so basically I want to open a sas script format the script using EG and then close. Is there any way you might be able to help me with such a script
    Thank you in advance.

    • Chris Hemedinger Chris Hemedinger
      Posted May 30, 2013 at 8:42 am | Permalink

      Antonie,

      There isn't an EG API that you can use for this from PowerShell, but you can probably use a UI automation tool (such as AutoHotKey) to record a UI macro that you can repeat with multiple files.

One Trackback

  1. [...] Using Windows PowerShell to report on your SAS Enterprise Guide project files [...]

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>