SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Debugging and Testing DATA Steps
Lesson Summary

This page contains

I. Text Summary

To go to the page where a task, programming feature, or concept was presented, select a link.


Understanding DATA Step Processing
SAS processes the DATA step in two phases. During the compilation phase, each statement is scanned for syntax errors and the program data vector is created. If the DATA step compiles successfully, then the execution phase begins. Each observation in the input data set is processed, stored in the program data vector, and then written to the new data set.

Automatic Variables
The program data vector contains two automatic variables that can be used for processing. _N_ counts the number of times that the DATA step has iterated. _ERROR_ signals the occurrence of an error caused by the data during execution. The default value is 0, which means there is no error; when an error occurs, the value is set to 1.

The SAS Log
A note in the SAS log displays the number of observations and variables in any new data set.

Diagnosing Syntax Errors
Missing or invalid punctuation or misspelled keywords are treated as syntax errors. These are detected during the compilation phase and underlined in the SAS log.

Diagnosing Execution-Time Errors
Illegal mathematical operations or processing character variables as numeric are considered execution-time errors. These are detected after compilation, during the execution of the DATA step. Notes in the SAS log can help you find and fix them.

Writing a NULL Data Set
You can test your DATA step program by submitting it without creating observations. Just use the keyword _NULL_ as the data set name in the DATA statement.

Limiting Observations
Another way to test a DATA step is to set the OBS= system option to 0 before processing. The OBS= option limits the number of observations read or created during execution of the DATA step. Remember to reset OBS= to MAX after you finish testing.

PUT Statement
When the source of program errors is not apparent, try using the PUT statement to examine variable values and generate your own message in the log. You can use a PUT statement with conditional processing to flag program errors or data that are out of range.


II. Syntax

To go to the page where a statement or option was presented, select a link.

DATA _NULL_;

OPTIONS OBS=0;

OPTIONS OBS=MAX;

PUT specification-1...specification-n;


III. Sample Programs
     data _null_;
        set finance.loans;
        Interest=amount*rate;
     run;

     options obs=0;
     data finance.newcalc;
        set finance.loans;
        Interest=amount*rate;
     run;

     data test;
        if code='1' then Type='Variable';
        else if code='2' then Type='Fixed';
        else put 'MY NOTE: invalid value: '
             code=;
     run;

     data finance.newcalc;
        set finance.loans;
        if rate>0 then
           Interest=amount*(rate/12);
        else put 'DATA ERROR ' rate= _n_=;
     run;


IV. Points to Remember
  • The term program data vector refers to a logical concept and does not necessarily reflect the physical storage of data.

  • When SAS encounters a syntax error, it tries to interpret your code. If the syntax error can be interpreted, the DATA step continues to be processed.

  • Most execution-time errors produce warning messages, but allow the SAS program to continue executing.

  • Reset the OBS= option to MAX after testing your DATA step with OBS=0.

  • If you use the PUT statement as a diagnostic tool, place text strings within quotation marks.

  • When you specify a variable name in a PUT statement, only its value is written to the log. To write both variable name and value to the log, specify the variable followed by an equal sign.


back||next

 

Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement