Desktop productivity for business analysts and programmers

Calling SAS from a script

Reply
Frequent Contributor
Posts: 97

Calling SAS from a script

I found a fantastic article on calling SAS from a VBScript - either in EG or batch.  I am able to get either to work to run a program and get the log.  In the log, I can see that the datasets are created and the proc created output.  However, I can't programmatically access the dataset or the results.  I have looked over the object model, and everything looks like this should work fine.  The log says that 19 records were read for creating the new dataset, and 1 page was created when running a proc.  However, the code object's OutputDatasets and Results collection both have a count of 0.  Idea suggestions would be greatly appreciated.  If it helps, I am running SAS 9.2, EG 4.3.  Here is the code.  Sorry, I couldn't find the <code> tags.

server = "\\nasvs1\Shared\4700Mac\shared2\Actuarial\SAS\"

projname = server & "Testing.egp"

codefiles = Array("LIBNAMES", "WORK")

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

Set oproj = oapp.Open(projname,"")

Set ocodecollect = oproj.CodeCollection

For Each code In codefiles

  For Each ocode In ocodecollect

  If ocode.Name = code Then

  ocode.Log.SaveAs server & ocode.Name & ".log"

  ocode.Run

  msgbox ocode.Name & ", OutputDatasets: " & ocode.OutputDatasets.Count

  msgbox ocode.Name & ", Results: " & ocode.Results.Count

  End If

  Next

Next

oproj.Save

oproj.Close

oapp.Quit

Christopher Johnson
www.codeitmagazine.com
Super User
Posts: 7,386

Re: Calling SAS from a script

If the SAS code actually creates a dataset in the library WORK, you won't be able to find anything, because WORK is dynamically created everytime a SAS session starts and deleted when it ends.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 97

Re: Calling SAS from a script

Wow.  I had no idea.  That did the trick.  You could not see what code I was running in SAS (my mistake), but I was creating dataset in work, running proc means, but the code object did not register any dataset or results.  When I switched to a permanent library, it worked. Thanks!

Christopher Johnson
www.codeitmagazine.com
Frequent Contributor
Posts: 121

Re: Calling SAS from a script

It looks like you are using the EG Automation API. Using this API you can only use the possibilities that the EG API gives you.

Another approach would be to use the SAS Integration Technologies API (see http://support.sas.com/documentation/cdl/en/itechwcdg/61500/PDF/default/itechwcdg.pdf).

Or you give PowerShell and the Cmdlets4Sas a try: Cmdlets4Sas / Wiki / Building a poor man&#39;s enterprise guide

It would also help other people to help you if you would tell what you try to achieve.

Frequent Contributor
Posts: 97

Re: Calling SAS from a script

Thanks!  My goal was just a sample script on my system where I could programmatically interact with the datasets and results.  I appreciate the link to the other API.

Christopher Johnson
www.codeitmagazine.com
Frequent Contributor
Posts: 97

Re: Calling SAS from a script

Sorry to come back to this, but I think I have found the source of my problems.  I run this code in VBScript using the SAS EG API.  The first time I run it, the code file has no datasets or results.  The second time I run it (and subsequent), it does.  If I open the project and delete the dataset and result shortcut, and I rerun the script, it has no results again.  I would seem that even though I am running the code and then trying to access the dataset and results, they aren't there until after the project is saved, and then they can be accessed by the shortcuts.  Any thoughts on this?

ocode.UseApplicationOptions = False

ocode.GenListing = True

ocode.GenSasReport = False

ocode.Log.SaveAs server & "Results\" & ocode.Name & ".log"

ocode.Run

ocode.SaveAs server & "Results\" & ocode.Name & ".sas"

msgbox ocode.Name & ", OutputDatasets: " & ocode.OutputDatasets.Count

msgbox ocode.Name & ", Results: " & ocode.Results.Count

For n=0 to (ocode.OutputDatasets.Count -1)

  dataName = ocode.OutputDatasets.Item(n).Name

  ocode.OutputDatasets.Item(n).SaveAs server & "Results\" & dataName & ".xls"

Next

For n=0 to (ocode.Results.Count -1)

  ocode.Results.Item(n).SaveAs server & "Results\" & WScript.ScriptName & n & ".lst"

Next

Christopher Johnson
www.codeitmagazine.com
Ask a Question
Discussion stats
  • 5 replies
  • 354 views
  • 0 likes
  • 3 in conversation