Your SAS programs, embedded in web apps and elsewhere

Error Handling in SAS Stored Process Web Application and PROC JSON

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Error Handling in SAS Stored Process Web Application and PROC JSON

I have a series of stored processes that perform tasks are are called from a Web application via the Stored Process Web App.  I have error checking in the code and want to return a JSON string with error messages instead of the whole SAS log.  My web developers don't need to see the SAS log or to have to parse it.  They just need some basic information.

 

Found this which puts me on the right track, but it doesn't seem to work with PROC JSON.

 

My macro is as follows:

%macro status_output(_webout);
%let syserr=0;
%let syscc=0;

proc json out=&_webout pretty nosastags;
	write values "STATUS" "&rc";
	
	%if %sysevalf(&rc > 0) %then %do;
		write values "ERROR";
		write open object;
			write values "ERROR_MSG" "&ERROR_MSG";
			write values "ERROR_REASON" "&ERROR_REASON";
		write close;
	%end;
run;
%mend;

However, when an error occurs, I still get the SAS Log and not my nice error handling.    

 

I do see this in the log:

7435      +%status_output(_webout);
MPRINT(STATUS_OUTPUT):   proc json out=_webout pretty nosastags;
MPRINT(STATUS_OUTPUT):   write values "STATUS" "1008";
MPRINT(STATUS_OUTPUT):   write values "ERROR";
MPRINT(STATUS_OUTPUT):   write open object;
MPRINT(STATUS_OUTPUT):   write values "ERROR_MSG" "In insert_variable - RC:1008";
MPRINT(STATUS_OUTPUT):   write values "ERROR_REASON" "Error Adding Variable - ORACLE: ORA-00001: unique constraint (IF_A2RPT.FACTOR_VARIABLES_UK1) violated;
MPRINT(STATUS_OUTPUT):   write close;
MPRINT(STATUS_OUTPUT):   run;

WARNING: RUN statement ignored due to previous errors. Submit QUIT; to terminate the procedure.NOTE: PROCEDURE JSON used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

There is no ERROR in PROC JSON, which leads me to believe it might be checking another system macro variable.

 

Curiously, if I execute the code from SAS Studio (writing to a different file than _webout), this run without problem and the file is populated beautifully.  This leads me to also believe there is something I can set to override the annoying behavior.

 

Anyone know what I need to do?


Accepted Solutions
Solution
‎03-27-2017 02:43 PM
New Contributor
Posts: 3

Re: Error Handling in SAS Stored Process Web Application and PROC JSON

Posted in reply to DominicPazzulaF

Added 

	option NOSYNTAXCHECK ;

Which solved the problem.

View solution in original post


All Replies
Solution
‎03-27-2017 02:43 PM
New Contributor
Posts: 3

Re: Error Handling in SAS Stored Process Web Application and PROC JSON

Posted in reply to DominicPazzulaF

Added 

	option NOSYNTAXCHECK ;

Which solved the problem.

PROC Star
Posts: 1,322

Re: Error Handling in SAS Stored Process Web Application and PROC JSON

Posted in reply to DominicPazzulaF

If the error was severe enough for SAS to enter syntaxcheck mode, you would see a note in the log to that effect.  "SAS set obs=0 blah blah".  And no code after that will be excuted.

 

My guess is in SAS Studio syntaxcheck mode is off by default.

 

In the stored process server, you could turn off syntaxcheck with:

options nosyntaxcheck;

but there are risks to doing that (if you have an error in code and end up with a 0 obs dataset, it might overwrite other datasets with 0 obs).

 

I think the better approach is to have your error handling macro recover from syntaxcheck mode. You can recover with:

%* if obs=0 assume that means SAS is in syntaxcheck mode, so recover;
%if %sysfunc(getoption(obs))=0 %then %do;
  options obs=max replace NoSyntaxCheck;
%end;
New Contributor
Posts: 3

Re: Error Handling in SAS Stored Process Web Application and PROC JSON

Exactly what I did.  The output and PROC JSON is always the last step, so I just set NOSYNTAXCHECK without checking the status.  There is nothing else to worry about.  The stored process streams the JSON and exits.

Frequent Contributor
Posts: 106

Re: Error Handling in SAS Stored Process Web Application and PROC JSON

Posted in reply to DominicPazzulaF

Hi, 

 

This is really useful. For what it's worth we handle this slightly differently in our Data Adapter, by checking input validity up front using SAS code, and if something's not right, writing a similar JSON message and issuing an ENDSAS statement to quit the rest of the program execution. Where an error occurs we handle the output at the frontend and keep the logs in an array, rather than suppressing the SAS log.

 

This is really useful for making SAS/ACCESS STPs more fault tolerant though. Thanks again.

 

Nik 

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 225 views
  • 7 likes
  • 3 in conversation