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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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