SAS Enterprise Guide

Desktop productivity for business analysts and programmers
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
diwakar_atwal
Fluorite | Level 6

Hello,

I'm trying to run a sas program using vbscript. I'm running vbscript on command prompt using cscript.exe

Below is my vbscript code, where I'm just having "abcdefg" in sas program (to test). Currently I'm testing this to get error message on command prompt, but instead there is no error displayed on command prompt. I can see error message in the log though.


Option Explicit
Dim app
Dim str
Dim prjObject1
Dim fso

str = "abcdefg"

Call dowork(str)

 

'shut down the app
If not (app Is Nothing) Then
app.Quit
Set app = Nothing
End If

 

Sub dowork(codestr)
On Error Resume Next
'----
' Start up Enterprise Guide using the project name
'----
Dim prjName
Dim prjObject

Set app = CreateObject("SASEGObjectModel.Application.7.1")
If Checkerror("CreateObject") = True Then
Exit Sub
End If

Set prjObject = app.New

Set fso = CreateObject("Scripting.FileSystemObject")
Set prjObject1 = prjObject.CodeCollection.Add

prjObject1.GenListing = True
prjObject1.GenSasReport = True

prjObject1.Text = codestr

'-----
' run the project
'-----
prjObject1.run
If Checkerror("Code.run") = True Then
Exit Sub
End If

' Save the log file to LOCAL disk
prjObject1.Log.SaveAs(Wscript.ScriptName & ".log")

End Sub

Function Checkerror(fnName)
Checkerror = False

Dim strmsg
Dim errNum

If Err.Number <> 0 Then
strmsg = "Error #" & Hex(Err.Number) & vbCrLf & "In Function " & fnName & vbCrLf & Err.Description
MsgBox strmsg 'Uncomment this line if you want to be notified via MessageBox of Errors in the script.
wscript.echo(strmsg)
wscript.quit(4)
Checkerror = True
End If

End Function

 

ERROR MESSAGE IN LOG:

abcdefg
ERROR 180-322: Statement is not valid or it is used out of proper order.


Is there any way to have this error returned in command prompt ? Any help is really appreciated.

Thank You.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

SAS only delivers return codes when run in batch. You can add a check to your vbscript that scans for log lines with WARNING when rc = 1, and WARNING and ERROR when rc = 2.

IMO catching a non-zero return code and alerting the person responsible should be sufficient; consulting the log is still the domain of human programmers.

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

SAS only delivers return codes when run in batch. You can add a check to your vbscript that scans for log lines with WARNING when rc = 1, and WARNING and ERROR when rc = 2.

IMO catching a non-zero return code and alerting the person responsible should be sufficient; consulting the log is still the domain of human programmers.

diwakar_atwal
Fluorite | Level 6

Thank you Kurt for your reply. I followed your suggestion and found a solution to my issue. I've added following lines of code into my vbscript.

 

Dim logfileobj
Dim logfile

 

Set logfileobj = fso.OpenTextFile(Wscript.ScriptName & ".log", 1)

logfile = logfileobj.ReadAll

 

If InStr(1,logfile, "ERROR", 1) <> 0 then
    wscript.quit(2)
end if

 

This will read the log file and search for the word ERROR (added a parameter '1' for case insensitive search). If found, then it will return the execution to command prompt with ERRORLEVEL = 2.

 

Thank You.

  

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2073 views
  • 1 like
  • 2 in conversation