Friday, August 7, 2009

Error Processing


Syntax Check Mode
If a DATA step has a syntax error, SAS can enter syntax check mode. SAS internally sets the OBS= option to 0 and the REPLACE/NOREPLACE option to NOREPLACE. When these options are in effect, SAS
  • reads the remaining statements in the DATA step
  • checks that statements are valid SAS statements
  • executes global statements
  • identifies any other errors that it finds
  • creates the descriptor portion of any output data sets that are specified in program statements
  • does not write any observations to new data sets that SAS creates
  • does not execute most of the subsequent DATA steps or procedures in the program (exceptions include PROC DATASETS and PROC CONTENTS).
Note: Any data sets that are created after SAS has entered syntax check mode do not replace existing data sets with the same name. [cautionend]


How Different Modes Process Errors
When SAS encounters most syntax or semantic errors, SAS underlines the point where it detects the error and identifies the error by number. If SAS encounters a syntax error when you run noninteractive SAS programs or batch jobs, it enters syntax check mode and remains in this mode until the program finishes executing.
When you run SAS in interactive line mode or in a windowing environment, syntax check mode is in effect only during the step where SAS encountered the error. When the system detects an error, it stops executing the current step and continues processing the next step.

Processing Multiple Errors
Depending on the type and severity of the error, the method you use to run SAS, and your operating environment, SAS either stops program processing or flags errors and continues processing. SAS continues to check individual statements in procedures after it finds certain kinds of errors. Thus, in some cases SAS can detect multiple errors in a single statement and may issue more error messages for a given situation, particularly if the statement containing the error creates an output SAS data set.
The following example illustrates a statement with two errors:
data temporary;
Item1=4;
run;

proc print data=temporary;
var Item1 Item2 Item3;         
run;
SAS Log: Multiple Program Errors

cpu time            0.00 seconds

1  data temporary;
2     Item1=4;
3  run;
NOTE: The data set WORK.TEMPORARY has 1 observations and 1
variables.
NOTE: DATA statement used:
real time           0.10 seconds
cpu time            0.01 seconds

4 
5  proc print data=temporary;
ERROR: Variable ITEM2 not found.
ERROR: Variable ITEM3 not found.
6     var Item1 Item2 Item3;
7  run;
NOTE: The SAS System stopped processing this step because of
errors.
NOTE: PROCEDURE PRINT used:
real time           0.53 seconds
cpu time            0.01 seconds
SAS displays two error messages, one for the variable Item2 and one for the variable Item3.
When running debugged production programs that are unlikely to encounter errors, you may want to force SAS to abend after a single error occurs. You can use the ERRORABEND system option to do this.

Using System Options to Debug a Program
You can use the following system options to control error handling (resolve errors) in your program:
BYERR controls whether SAS generates an error message and sets the error flag when a _NULL_ data set is used in the SORT procedure.
DKRICOND= controls the level of error detection for input data sets during the processing of DROP=, KEEP=, and RENAME= data set options.
DKROCOND= controls the level of error detection for output data sets during the processing of DROP=, KEEP=, and RENAME= data set options and the corresponding DATA step statements.
DSNFERR controls how SAS responds when a SAS data set is not found.
ERRORABEND specifies how SAS responds to errors.
ERRORCHECK= controls error handling in batch processing.
ERRORS= controls the maximum number of observations for which complete error messages are printed.
FMTERR determines whether SAS generates an error message when a format of a variable cannot be found.
INVALIDDATA= specifies the value that SAS assigns to a variable when invalid numeric data is encountered.
MERROR controls whether SAS issues a warning message when a macro-like name does not match a macro keyword.
SERROR controls whether SAS issues a warning message when a defined macro variable reference does not match a macro variable.
VNFERR controls how SAS responds when a _NULL_ data set is used.
For more information, see "SAS System Options" in SAS Language Reference: Dictionary.

Using Return Codes
In some operating environments SAS passes a return code to the system, but accessing return codes is specific to your operating environment.
Operating Environment Information: For more information about return codes, see the SAS documentation for your operating environment. [cautionend]

Other Error Checking Options
To help determine your programming errors, you can use:

Error-Handling Techniques


How FEEDBACK= Differs from _IORC_ and _FDBK_
The FEEDBACK= option specifies a SAS variable that is set to the VSAM feedback code. The variable is set only when VSAM encounters a logical error. That is, the variable's value is 0 until a logical error occurs. The nonzero value indicates what type of logical error was detected. Some Common Causes of Logical Errors describes the feedback codes that are most likely to be returned in the FEEDBACK= variable.
Note that both the _FDBK_ and the FEEDBACK= variables are set to the VSAM feedback code when a logical error occurs. The distinction between the two values is that only by specifying the FEEDBACK= variable (and resetting it) can you continue to process and detect later errors that might occur. The ability to reset the FEEDBACK= variable after taking appropriate action to handle the error is very significant. For this reason, it is strongly recommended that you use the FEEDBACK= option for all VSAM data sets in which logical errors might occur.
Other distinctions are that _FDBK_ is also set if the following occurs:

  • when VSAM detects a physical error.

  • when VSAM sets a zero return code in certain situations.
    You get a nonzero _FDBK_ with a zero _IORC_ when you try to create a duplicate key in an alternate index.
The FEEDBACK= variable has a nonzero value only when a logical error occurs.

Using the FEEDBACK= Option
The FEEDBACK= option in the INFILE statement specifies a SAS variable that is set to the VSAM feedback code when VSAM detects a logical error. You can determine what caused the error by inspecting the FEEDBACK= variable value. You can then design program logic that takes appropriate action depending on the value of the FEEDBACK= variable. You must reset the values of both the FEEDBACK= variable and the _ERROR_ variable to 0 in order to continue processing.
Resetting the variable to 0 enables you to continue processing in a meaningful way. That is, you can continue both to read and write records and detect other errors in the DATA step. If you do not reset the FEEDBACK= and _ERROR_ variables before the next INPUT or PUT statement, SAS assumes that your program cannot handle the error condition, and it executes the following:

  1. prints a message that includes information about the data set and the VSAM logical error code on the SAS log

  2. terminates the DATA step
The DATA step also terminates when the FEEDBACK= option is not specified, and a logical error occurs while it attempts to write with a PUT statement.
You must use the FEEDBACK= option to use the key-testing techniques for a KSDS (described in Processing a KSDS in a SAS Job) and the slot-testing techniques for an RRDS (described in Processing an RRDS in a SAS Job).
VSAM cannot return data to the input buffer when there is a logical or physical I/O error. Subsequent INPUT statements cannot read from an empty INPUT buffer, which leaves variables without values. To avoid this situation, test the values of _IORC_ and the FEEDBACK= variable by using a trailing @ with the INPUT statement that initiates the VSAM read request:
infile indata vsam feedback=NOERROR;
input @;
/* Read: look at values of FEEDBACK= variable */
/* and _IORC_.  If OK, finish reading values  */
/* into variables and write them to the SAS   */
/* print file.                                */
if _IORC_ = 0 and NOERROR=0 then do;
      input var1 $ var2 var3 $;
      file print;
      put var1 var2 var3;
end;
/* If _IORC_ and NOERROR=0 */
else if _IORC_= 12 then do;
/* Physical error has occurred.             */
/* INPUT buffer is empty: nothing to read.  */
      _ERROR_ = 0;
/* Reset the _ERROR_ variable.              */
      file log;
/* Write message on the SAS log.            */
      put 'Physical error has occurred for observation ' _N_ '.'
      'I/O return code is ' _IORC_ '.';
      input;
/* Ignore blank buffer: release trailing @. */
      return;
end;
/* Else: _IORC_=12 */
else if NOERROR ^= 0 then do;
/* Logical error has occurred.              */
/* INPUT buffer is empty: nothing to read.  */
      _ERROR_ = 0;
      file log;
/* Write message on the SAS log.            */
      put 'Logical error has occurred for observation ' _N_ '.'
      'Feedback code is ' noerror '.';
      NOERROR=0;
/* Reset FEEDBACK= variable back to 0.       */
      input;
/* Ignore blank buffer: release trailing @   */
      _ERROR_ = 0;
/* Above INPUT stmt. sets both the _ERROR_   */
      NOERROR=0;
/* and the FEEDBACK= variables. Both need    */
/* to be reset to 0 again.                   */
      return;
end;
/* Else: NOERROR ^= 0 */
...more SAS statements...
Using the INPUT @ statement gives you the opportunity to examine the FEEDBACK= variable for a nonzero value, which indicates that a logical error has occurred. If both the _IORC_ and the FEEDBACK= variables are zero, continue with the INPUT statement to read data into variables.
Notice that the _ERROR_ and the FEEDBACK= variable, NOERROR, need to be reset to 0 twice when set to a nonzero value by an INPUT statement with a trailing @. They need to be reset to 0 the first time in order to continue processing. The processing continues by releasing the held record from the input buffer with an INPUT statement without a trailing @. This sets the _ERROR_ and FEEDBACK= variables to nonzero values again; therefore, they need to be reset to 0 a second time in order to proceed.
You might want to print error messages warning you that either a physical error was encountered (if _IORC_ is 12) or a logical error was encountered (if the FEEDBACK= variable is not 0). You might also design logic to handle specific, anticipated FEEDBACK= variable values.

Tuesday, August 4, 2009

SASHELP / DICTIONARY

VCATALG / CATALOGS
SAS catalogs

VCOLUMN / COLUMNS
Data set columns and attributes

VEXTFL / EXTFILES
Allocated filerefs and external physical paths

VINDEX / INDEXES
Data set indexes

VMACRO / MACROS
Global and automatic macro variables

VMEMBER / MEMBERS
SAS data sets and other member types

VOPTION / OPTIONS
Current SAS System option settings

VTABLE / TABLES
SAS data sets and views

VTITLE / TITLES
Title and footnote definitions

VVIEW / VIEWS
SAS data views

CAT/CATS/CATT/CATX Functions

These Functions and Call Routines can be used to join two or more strings together.
Even though we can use the concatenation operator in combination with the STRIP, TRIM, or LEFT functions, these functions make it much easier to put strings together and if you wish, to place one or more separator characters between the strings.
One advantage of using the call routines than the functions is improved performance.
Note: *Call routine executes faster than the function… in specific…

CALL CATS:
To concatenate two or more strings, removing both leading and trailing blanks.
CATS () stands for concatenate and strip.
Just think the ‘S’ at the end of the CATS as “Strip Blanks”.
Syntax: CALL CATS (result, string-1<, string-n>);

Example:

A=”ABC”;
B=” CONSULTING”;
C=”INC “;
D=” TEAM “;

FUNCTION= RESULT
CALL CATS(RESULT, A,B)= "ABCCONSULTING"
CALL CATS(RESULT, A,B,C)= "ABCCONSULTINGINC"
CALL CATS(RESULT, "HELLO",D)= "HELLOTEAM"

CALL CATT:
To concatenate two or more strings, removing only trailing blanks.
Just think the ‘T’ at the end of the CATT as “Trailing Blanks” or “Trim Blanks”.
Syntax: CALL CATS (result, string-1<, string-n>);

Example:

A=”ABC”;
B=” CONSULTING”;
C=”INC “;
D=” TEAM “;

FUNCTION= RESULT
CALL CATT(RESULT, A,B) ="ABC CONSULTING"
CALL CATT(RESULT, A,B,C)= "ABC CONSULTINGINC"
CALL CATT(RESULT, "HELLO",D)= "HELLO TEAM"

CALL CATX:
To concatenate two or more strings and removing both leading and trailing blanks and places a single space, or one or more characters of our choice, between each strings.
Just think the ‘X’ at the end of the CATX as “add eXtra blank.”
Syntax: CALL CATX (separator, result, string-1<, string-n>);

Example:

A=”ABC”;
B=” CONSULTING”;
C=”INC “;
D=” TEAM “;

FUNCTION =RESULT
CALL CATX(" ",RESULT, A,B) ="ABC CONSULTING"
CALL CATX(" ,", RESULT, A,B,C) ="ABC,CONSULTING,INC"
CALL CATX(' / ', RESULT, "HELLO",D) ="HELLO/TEAM"
CALL CATX(' *** ', RESULT, "HELLO",D) = "HELLO***TEAM"