2
votes

I've searched a bit for a way to execute a SAS Enterprise Guide project and its programs via Python but I could not find anything. I only find SAS examples like the question: How do I invoke a sas script in python?

I can call the enterprise guide project as my sysin like the answer in that link suggested, but when the project opens it tries to import all the other parameters I passed. I also can't find the path for each individual SAS program as it is inside the project.

Does anyone knows a way to simply run the SAS programs within a project via python?

Thanks

3

3 Answers

2
votes

I have the same complaint as you, so I created a tool to execute egp file from python or command line. https://github.com/kjnh10/saseg_runner

I hope this would help.

0
votes

You'll have to save your SAS Enterprise Guide program simply as a SAS program and run it as a SAS script using SAS in batch mode.

SAS Enterprise Guide is a visual editor, but behind the scenes it's still creating SAS code.

0
votes

EG has a COM interface that can be automated with python, as is in user3394926's git repo below.

Chris Hemedinger from SAS wrote a great article on the SAS communities website, he's cjdinger on github and has a few vb scripts that show how to access the various COM interfaces. The current link to the SAS Article written in 2017 is here: https://communities.sas.com/t5/SAS-Communities-Library/Doing-More-with-SAS-Enterprise-Guide-Automation/ta-p/417832?title=Not_Just_for_Scheduling:_Doing_More_with_SAS_Enterprise_Guide_Automation

Note that there is a link to the documentation reference guide available as a CHM that lists each object and its methods and properties in that article.

You can also export an EG script as a VB script to be scheduled using Windows Scheduler, you'll see some of the various COM interfaces and functions there.

A basic execution is:

import win32com.client as win32

eg = win32.Dispatch('SASEGObjectModel.Application.7.1')

example_file = r'./my_eg_file.egp'

project = eg.Open(example_file, "")
project.Run()
project.SaveAs(example_file) # Note that this will overwrite, comment out or rename if you don't want that. 
project.Close()

Take a look at the examples cjdinger has. Some important details:

  • Most of the interfaces have the Name property

  • A standard EG project has "Flows" which contain "Items" which are of different types

  • There are 26 different types of items in a given project, each with its own interface (not well documented...)

  • The Types that cjdinger handled in his "ExtractCodeAndLog.vbs" example are 0: egFile, 1: egCode, 2: egData, 3: egTask, and 4: egQuery

  • project.ContainerCollection - is a collection of the "Process Flows" that can be iterated over, I'm not an EG power user, but most of my projects have 2, one with all my steps and an empty one

  • <flow>.Items - is another collection but of the items in a specific flow, these are the main steps, however egData can have child Tasks which are also shown as steps in the EG GUI

eg:

flows = project.ContainerCollection
for flow in flows:
    print(f'FLOW: {flow.Name}')
    for item in flow.Items:
        print(f'ITEM: {item.Name} is type ID {item.Type}')
        if item.Type in (2,):   # egData type
            for task in item.Tasks:
                print(f'TASK: {task.Name} is a task of {item.Name} is type {task.Type}')
  • Text needs to be accessed from the Text property of the various object

  • Most items have a Log property you can access the text of using item.Log.Text, except egData which have Tasks that have the log, you may need to run the project before you get a log, if there is no Log it throws an error.

  • if there is Code, you can access it through the item.TaskCode object and item.TaskCode.Text is the string property for printing/writing

Good luck, I can try to help with any other questions. I like the runner.py script that user3394926 has written to detect errors using an re search for ERROR in the logs. I've implemented a similar feature in mine.