Link to home
Start Free TrialLog in
Avatar of labradorchik
labradorchikFlag for United States of America

asked on

SAS Error codes/variables

SAS codes (SYSCC, SYSRC, and SYSERR) indicate different log results when SAS programs run in UNIX or PC SAS.  Which is the best or most efficient way to use these codes?

This is how I use SAS macro exit codes in my SAS:

data _null_;
   X "echo &syscc > progerr.txt";
run;

%put syserr= &syserr syscc = &syscc sysrc = &sysrc;

Open in new window


I would like to hear how the rest of you handle SAS exit/return codes?
Avatar of Ian
Ian
Flag of Australia image

Hi there : labradorchik,

The whole point of error codes is to allow conditional processing under program control.

As an example of using various SAS return codes, the following macro
{% DefineLib(ref, path)  }
allocates a library to a defined location.

Essentially it does a
libname ref "path";
but under script control so that it ensures that any calling script won't proceed if the library cannot be allocated for any reason eg network drive not allocated, the folder location deleted, .... Note that it produces a LOG message and returns without failing if the libname already is allocated to a library.  You could easily change this behavior.

It runs in an environment of enterprise guide which does ODS output to HTML. Hence the possible error report is generated into the output stream as HTML.

It is set up as an autocall sas macro which puts a one line note in the log when it is defined (on first use).

/* DefineLib.sas */
%*************************************************************
Define a library, if not already defined
************************************************************;

%macro DefineLib(ref, path);
%local RC;			%* Return code from SysFunc ;
%local SYSMSGtext;		%* message generayed by SysFunc ;

%* Establish if the library does not exist or cannot be accessed;
%let RC = %sysfunc(libref(&ref));  %* 0 -> assigned, 7006 -> not assigned *;
%let SYSMSGtext = %sysfunc(sysmsg());
%* put &RC &ref &SYSMSGtext;

%if (&RC = 0) %then
	%do;
	%put NOTE: libname &ref is already defined. Use  libname &ref clear%str(;)  before a new definition.;
	%end;
%else
	%do;

	%if (%Qsubstr(&path, 1, 1) ^= %str(%")) and
		(%Qsubstr(&path, 1, 1) ^= %str(%() ) %then
		%let path = "&path";

    %* Regardless set up a display value (left justified) AND with all the double quote values etc masked;
	%let displayPath = %qleft(&path);

	libname &ref &path;
	
	* Now try again;
	%let RC = %sysfunc(libref(&ref));	%* 0 is good return *;
	%let SYSMSGtext = %sysfunc(sysmsg());
	%if (&RC = 0) %then
		%do;
		%* put Libname &ref has now been assigned to &path;
		%end;
	%else
		%do;
		%* Cannot allocate the library ;
        libname &ref clear;         *** Get rid of the bad definition ***;
		
		%let _STPERROR=1;

		data _null_;
			file print;
			*************************************************************;
			** IN following lines if an html tag is not at the start of line **
			** then SAS converts all internal tags to &lt xxx &gt.!!	**
*************************************************************;

			put "</pre>";			* SAS encloses output in <pre> and </pre> ;
		
			put "<h1>Library &ref cannot be accessed</h1>";
			put "<p>";
			put "The library";
			put "<strong> &ref </strong>";
			put " is required by this program and";
            put "<br />";
            PUT "could not be accessed at ";
			put "<strong> &displayPath. </strong>";
			put "</p><p>";
			put "The error code and message encountered was:";
			put "</p><p>";
			put "&RC ~ &SYSMSGtext ";
			put "</p><p>";
			put 'Please contact your local SAS guru,';
			put "  Phone 123 456 789  about this problem.";
			put "</p><p>";
			put "Email: ";
			put "<a href='mailto:sasGuru@coName.domain.org?subject=library reference problem on web page'>";
			put "mailto:sasGuru@coName.domain.org";
			put "</a>";
			put "</p>";

			put "<pre>";			* SAS encloses output in <pre> and </pre> ;
		run;

		options OBS=0;
        %put NOTE: library &ref cannot be allocated to &displayPath Script will now stop;
		data _null_; ABORT; run;
		%end;

	%end;

%mend DefineLib;
%put macro DefineLib(ref, path) - Conditionally define library - has been defined;

Open in new window


The usefullness of the return codes is to actually use the return code to conditionally do something  in the macro code.  Just printing out the RC value is a bit lame.

Regards,

Ian
Avatar of labradorchik

ASKER

ShannonEE, thank you very much for your explanations!!
I had to read a few times to grasp the whole idea behind it. :)
I guess the only question I have is which return error code is most efficient to use?
SYSCC, SYSRC, or SYSERR?
ASKER CERTIFIED SOLUTION
Avatar of Ian
Ian
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi ShannonEE,
Sorry for the late response but I had to do some testing in order to understand this process fully. Everything now makes sense to me. Thank you very much for all your explanations! I really appreciate your help!!