Correcting Common Errors

The Basics of Error Correction

To correct simple errors, such as the spelling error here, type over the incorrect text, delete text, or insert text. In the following program, the incorrect spelling of PRINT in the PROC step is corrected.
data work.admitfee;
  set cert.admit;
run;
proc print data=work.admitfee;
  var id name actlevel fee;
run;

Resubmitting a Revised Program

After correcting your program, you can resubmit it.
Figure 5.1 Correct PRINT Procedure Output
Correct PROC PRINT Output
Remember to check the SAS log again to verify that your program ran correctly.
Log 5.2 SAS Log: No Error Messages
9231  data work.admitfee;
9232      set cert.admit;
9233  run;

NOTE: There were 21 observations read from the data set CERT.ADMIT.
NOTE: The data set WORK.ADMITFEE has 21 observations and 9 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


9234  proc print data=work.admitfee;
9235      var id name actlevel fee;
9236  run;

NOTE: There were 21 observations read from the data set WORK.ADMITFEE.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

The Basics of Logic Errors

The PUTLOG Statement

A logic error occurs when the program statements execute, but produce incorrect results. Because no notes are written to the log, logic errors are often difficult to detect. Use the PUTLOG statement in the DATA step to write messages to the SAS log to help identify logic errors.
Syntax, PUTLOG statement
PUTLOG 'message';
message specifies the message that you want to write to the SAS log. It can include character literals, variable names, formats, and pointer controls.
Note: You can precede your message text with WARNING, MESSAGE, or NOTE to better identify the output in the log.
The PUTLOG statement can be used to write to the SAS log in both batch and interactive modes. If an external file is open for output, use this statement to ensure that debugging messages are written to the SAS log and not to the external file.

Temporary Variables

The temporary variables _N_ and _ERROR_ can be helpful when you debug a DATA step.
Variable
Description
Debugging Use
_N_
The number of times the DATA step iterated
Displays debugging messages for a specified number of iterations of the DATA step
_ERROR_
Initialized to 0, set to 1 when an error occurs
Displays debugging messages when an error occurs

Example: The DATA Step Produces Wrong Results but There Are No Error Messages

The data set contains three test scores and homework grades for four students. The program below is designed to select students whose average score is below 70. Although the program produces incorrect results, there are no error messages in the log.
data work.grades; 
  set cert.class;
  Homework=Homework*2;
  AverageScore=MEAN(Score1 + Score2 + Score3 + Homework);
    if AverageScore<70;
run;
A glance at the data set shows that there should be students whose mean scores are below 70. However, the data set Work.Grades has zero observations and six variables.
NOTE: There were 4 observations read from the data set
      CERT.CLASS.
NOTE: The data set WORK.GRADES has 0 observations and 6
      variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds
Use the PUTLOG statement to determine where the DATA step received incorrect instructions. Place the PUTLOG statement before the subsetting IF.
PUTLOG Name= Score1= Score2= Score3= Homework= AverageScore=; 
29457  data work.grades;
29458      set cert.class;
29459      Homework=Homework*2;
29460      AverageScore=MEAN(Score1 + Score2 + Score3 +
29460! Homework);
29461      putlog Name= Score1= Score2= Score3= Homework=
29461! AverageScore=;
29462      if AverageScore<70;
29463  run;

Name=LINDA Score1=53 Score2=60 Score3=66 Homework=84
AverageScore=263
Name=DEREK Score1=72 Score2=64 Score3=56 Homework=64
AverageScore=256
Name=KATHY Score1=98 Score2=82 Score3=100 Homework=96
AverageScore=376
Name=MICHAEL Score1=80 Score2=55 Score3=95 Homework=100
AverageScore=330
NOTE: There were 4 observations read from the data set
      CERT.CLASS.
NOTE: The data set WORK.GRADES has 0 observations and 6
      variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds
Looking at the log, you can see the result of the PUTLOG statement. The data that is listed in the middle of the log shows that the variables were read in properly, and the variable Homework was adjusted to be weighted the same as Scores1–3. However, the values of AverageScore are incorrect. They are above the available maximum grade.
There is a syntax error in the line that computes AverageScore: Instead of commas separating the three score variables in the MEAN function, there are plus signs. Since functions can contain arithmetic expressions, SAS simply added the four variables together, as instructed, and computed the mean of a single number. That is why no observations had values of AverageScore below 70.
To fix the error, replace the plus signs in the MEAN function with commas. You can remove the PUTLOG statement and use a PROC PRINT statement to view your results.
data work.grades; 
  set cert.class;
  Homework=Homework*2;
  AverageScore = MEAN(Score1, Score2, Score3, Homework);
    if AverageScore < 70;   
run;
proc print data=work.grades;
run;
The figure below lists the names of students whose average score is below 70.
Figure 5.2 Corrected Program Output
Corrected Program Output

PUT Statement

Syntax

When the source of program errors is not apparent, you can use the PUT statement to examine variable values and to print your own message in the log. For diagnostic purposes, you can use IF-THEN/ELSE statements to conditionally check for values. For more information about IF-THEN/ELSE statements, see Creating and Managing Variables.
Syntax, PUT statement:
PUT specification(s);
specification specifies what is written, how it is written, and where it is written. Here are examples:
  • a character string
  • one or more data set variables
  • the automatic variables _N_ and _ERROR_
  • the automatic variable _ALL_

Example: Using the PUT Statement

The following example illustrates how to use the PUT statement to write messages to the SAS log.
data work.test;
  set cert.loan01;
  if code='1' then type='variable';                                /*#1*/
    else if code='2' then type='fixed';
    else type='unknown';
  if type='unknown' then put 'MY NOTE: invalid value: ' code=;     /*#2*/
run;
1 If the value of the variable Code equals 1, then the program returns the value for Type as variable. If the value equals 2, then the return value for Type is fixed. Otherwise, the value of Type is returned as unknown.
2 If Type contains the value unknown, then the PUT statement writes a message to the log.
Log 5.3 SAS Log
NOTE: Character values have been converted to numeric
      values at the places given by: (Line):(Column).
      148173:11   148174:18
MY NOTE: invalid value: Code=3
MY NOTE: invalid value: Code=3
MY NOTE: invalid value: Code=3

Example: Character Strings

You can use a PUT statement to specify a character string to identify your message in the log. The character string must be enclosed in quotation marks.
data work.loan01;
  set cert.loan;
  if code='1' then type='variable';
    else if code='2' then type='fixed';
    else type='unknown';
  put 'MY NOTE: The condition was met.';
run;
The following is printed to the SAS log.
Log 5.4 SAS Log
MY NOTE: The condition was met.
MY NOTE: The condition was met.
MY NOTE: The condition was met.
MY NOTE: The condition was met.
MY NOTE: The condition was met.
MY NOTE: The condition was met.
MY NOTE: The condition was met.
MY NOTE: The condition was met.
MY NOTE: The condition was met.
NOTE: There were 9 observations read from the data set
      CERT.LOAN.
NOTE: The data set WORK.LOAN01 has 9 observations and 6
      variables.

Example: Data Set Variables

You can use a PUT statement to specify one or more data set variables to be examined for that iteration of the DATA step.
Note: When you specify a variable in the PUT statement, only its value is written to the log. To write both the variable name and its value to the log, add an equal sign (=) to the variable name.
data work.loan01;
  set cert.loan;
  if code='1' then type='variable';
    else if code='2' then type='fixed';
    else type='unknown';
  put 'MY NOTE: Invalid Value: ' code=  type= ;
run;
The following is printed to the SAS log.
Log 5.5 SAS Log
MY NOTE: Invalid Value: Code=1 type=variable
MY NOTE: Invalid Value: Code=1 type=variable
MY NOTE: Invalid Value: Code=1 type=variable
MY NOTE: Invalid Value: Code=2 type=fixed
MY NOTE: Invalid Value: Code=2 type=fixed
MY NOTE: Invalid Value: Code=2 type=fixed
MY NOTE: Invalid Value: Code=3 type=unknown
MY NOTE: Invalid Value: Code=3 type=unknown
MY NOTE: Invalid Value: Code=3 type=unknown
NOTE: There were 9 observations read from the data set
      CERT.LOAN.
NOTE: The data set WORK.LOAN01 has 9 observations and 6
      variables.

Example: Conditional Processing

You can use a PUT statement with conditional processing (that is, with IF-THEN/ELSE statements) to flag program errors or data that is out of range. In the example below, the PUT statement is used to flag any missing or zero values for the variable Rate.
data work.newcalc;
  set cert.loan;
  if rate>0 then Interest=amount*(rate/12);
    else put 'DATA ERROR: ' rate= _n_ = ;
run;
The following is printed to the SAS log:
Log 5.6 SAS Log
DATA ERROR: Rate=. _N_=7
NOTE: There were 10 observations read from the data set
      CERT.LOAN.
NOTE: The data set WORK.NEWCALC has 10 observations and 5
      variables.

Missing RUN Statement

Each step in a SAS program is compiled and executed independently from every other step. As a step is compiled, SAS recognizes the end of the current step when it encounters one of the following statements:
  • a DATA or PROC statement, which indicates the beginning of a new step
  • a RUN or QUIT statement, which indicates the end of the current step
    Note: The QUIT statement ends some SAS procedures.
data work.admitfee;               /*#1*/
  set cert.admit;
proc print data=work.admitfee;    /*#2*/
  var id name actlevel fee;
                                  /*#3*/
1 Even though there is no RUN statement after the DATA step, the DATA step executes because the PROC step acts as a step boundary.
2 The PROC step does not execute. There is no following RUN statement for the step, nor is there a DATA or PROC step following the PROC PRINT step. Therefore, there is no indication that the step has ended.
3 The RUN statement is necessary at the end of the last step. If the RUN statement is omitted from the last step, the program might not complete processing and might produce unexpected results.
If you are programming in Enterprise Guide or SAS Studio, the system submits a RUN statement after every program that you submit, so the above program would execute normally.
Note: Although omitting a RUN statement is not technically an error, it can produce unexpected results. A best practice is to always end a step with a RUN statement.
To correct the error, submit a RUN statement at the end of the PROC step.
run;

Missing Semicolon

One of the most common errors is a missing semicolon at the end of a statement. Here is an example:
data work.admitfee;
  set cert.admit;
run;
proc print data=work.admitfee 
  var id name actlevel fee;
run;
When you omit a semicolon, SAS reads the statement that lacks the semicolon (along with the following statement) as one long statement.
Log 5.7 SAS Log: Error Messages
9240  proc print data=work.admitfee
9241      var id name actlevel fee;
          ---
          22
          76
ERROR 22-322: Syntax error, expecting one of the following: ;, (, BLANKLINE, CONTENTS, DATA,
              DOUBLE, GRANDTOTAL_LABEL, GRANDTOT_LABEL, GRAND_LABEL, GTOTAL_LABEL, GTOT_LABEL,
              HEADING, LABEL, N, NOOBS, NOSUMLABEL, OBS, ROUND, ROWS, SPLIT, STYLE, SUMLABEL,
              UNIFORM, WIDTH.
ERROR 76-322: Syntax error, statement will be ignored.
9242  run

Correcting the Error: Missing Semicolon

  1. Find the statement that lacks a semicolon. You can usually find it by looking at the underscored keywords in the error message and working backward.
  2. Add a semicolon in the appropriate location.
  3. Resubmit the corrected program.
  4. Check the SAS log again to make sure there are no other errors.

Unbalanced Quotation Marks

Some syntax errors, such as the missing quotation mark after HIGH in the program below, cause SAS to misinterpret the statements in your program.
data work.admitfee;
  set cert.admit;
  where actlevel='HIGH;
run;
proc print data=work.admitfee;
  var id name actlevel fee;
run;
When the program is submitted, SAS is unable to resolve the DATA step, and a DATA STEP running message appears at the top of the active window.
Tip
Both SAS Enterprise Guide and SAS Studio add a final line of code to stop unbalanced quotation marks.
Sometimes a warning appears in the SAS log that indicates the following:
  • A quoted string has become too long.
  • A statement that contains quotation marks (such as a TITLE or FOOTNOTE statement) is ambiguous because of invalid options or unquoted text.
When you have unbalanced quotation marks, SAS is often unable to detect the end of the statement in which it occurs. In Enterprise Guide or SAS Studio, simply add the balancing quotation mark and resubmit the program. However, in some environments, this technique usually does not correct the error. SAS still considers the quotation marks to be unbalanced.
Therefore, you need to resolve the unbalanced quotation mark by canceling the submitted statements (in the Windows and UNIX operating environments) or by submitting a line of SAS code (in the z/OS operating environment) before you recall, correct, and resubmit the program.

Correcting the Error in the Windows Operating Environment

  1. Press the Ctrl and Break keys or click the Break Icon Break Icon on the toolbar.
  2. Select 1. Cancel Submitted Statements, and then click OK.
    Tasking Manager Window
  3. Select Y to cancel submitted statements, and then click OK.
    Break Window
  4. Correct the error and resubmit the program.

Correcting the Error in the UNIX Environment

  1. Open the Session Management window and click Interrupt.
    Session Management Window
  2. Select 1. Cancel Submitted Statements, and then click Y.
    Tasking Management Window
  3. Correct the error and resubmit the program.

Correcting the Error in the z/OS Operating Environment

  1. Submit an asterisk followed by a single quotation mark, a semicolon, and a RUN statement.
    *'; run;
  2. Delete the line that contains the asterisk followed by the single quotation mark, the semicolon, and the RUN statement.
  3. Insert the missing quotation mark in the appropriate place.
  4. Submit the corrected program.
Tip
You can also use the above method in the Windows and UNIX operating environments.

Semantic Error: Invalid Option

An invalid option error occurs when you specify an option that is not valid in a particular statement. In the program below, the KEYLABEL option is not valid when it is used with the PROC PRINT statement.
data work.admitfee;
  set cert.admit;
  where weight>180 and (actlevel='MOD' or actlevel='LOW);
run;
proc print data=cert.admit keylabel;
  label actlevel='Activity Level';
run;
When a SAS statement that contains an invalid option is submitted, a message appears in the SAS log indicating that the option is not valid or not recognized.
Log 5.8 SAS Log: Syntax Error Message
9254  proc print data=cert.admit keylabel;
                                    --------
                                    22
                                    202
ERROR 22-322: Syntax error, expecting one of the following: ;, (, BLANKLINE, CONTENTS, DATA,
              DOUBLE, GRANDTOTAL_LABEL, GRANDTOT_LABEL, GRAND_LABEL, GTOTAL_LABEL, GTOT_LABEL,
              HEADING, LABEL, N, NOOBS, NOSUMLABEL, OBS, ROUND, ROWS, SPLIT, STYLE, SUMLABEL,
              UNIFORM, WIDTH.
ERROR 202-322: The option or parameter is not recognized and will be ignored.
9255      label actlevel='Activity Level';
9256  run;

NOTE: The SAS System stopped processing this step because of errors.

Correcting the Error: Invalid Option

  1. Remove or replace the invalid option, and check your statement syntax as needed.
  2. Resubmit the corrected program.
  3. Check the SAS log again to make sure there are no other errors.
Last updated: August 23, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.109.34