Desktop productivity for business analysts and programmers

Error not returned to command prompt when running sas program via vbscript

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Error not returned to command prompt when running sas program via vbscript

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.

 


Accepted Solutions
Solution
‎11-25-2017 07:00 PM
Super User
Posts: 9,611

Re: Error not returned to command prompt when running sas program via vbscript

Posted in reply to diwakar_atwal

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
‎11-25-2017 07:00 PM
Super User
Posts: 9,611

Re: Error not returned to command prompt when running sas program via vbscript

Posted in reply to diwakar_atwal

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 5

Re: Error not returned to command prompt when running sas program via vbscript

Posted in reply to KurtBremser

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.

  

Super User
Posts: 9,611

Re: Error not returned to command prompt when running sas program via vbscript

Posted in reply to diwakar_atwal

Since SAS itself returns with rc = 2 in case of an error, it might be sufficient to retrieve the SAS rc and use it in the exit of the script.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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