Correcting Common Errors

The Basics of Error Correction

To correct errors in programs, edit them in the code editing window. To correct simple errors, such as the spelling error here, type over the incorrect text, delete text, or insert text.
data sasuser.admitfee; 
   set sasuser.admit; 
run; 
proc prin data=sasuser.admitfee; 
   var id name actlevel fee; 
run;
In the program below, the missing letter t has been inserted into the SAS keyword that is specified in the PROC PRINT statement.
Figure 4.2 Corrected Program
Corrected Program

Resubmitting a Revised Program

After correcting your program, you can resubmit it. If you are working in the SAS windowing environment, it is a good idea to clear the messages from the Log window before resubmitting the program so that you do not confuse the old error messages with the new messages.
Figure 4.3 Correct PROC PRINT Output
Correct PROC PRINT Output
Remember to check the Log window again to verify that your program ran correctly.
Log 4.1 SAS Log: No Error Messages
9231  data sasuser.admitfee;
9232      set sasuser.admit;
9233  run;

NOTE: There were 21 observations read from the data set SASUSER.ADMIT.
NOTE: The data set SASUSER.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=sasuser.admitfee;
9235      var id name actlevel fee;
9236  run;

NOTE: There were 21 observations read from the data set SASUSER.ADMITFEE.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds
Tip
To resubmit a section of code in the Windows operating environment, highlight the selected code in the code editing window. Then press the F3 key on the keyboard.
CAUTION:
If you are programming in the SAS windowing environment, this and other chapters show you the Editor window only. If you are not using the Editor as a code editing window, be sure to adapt the directions for the Editor window.

The PUTLOG Statement

The Basics of Logic Errors

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 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.

Details

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 raw data file contains information from a class. For each student, there are three scores from tests, and one score from homework. 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; 
   infile class; 
   input Name $ Score1 Score2 Score3 Homework;
   Homework=Homework*2;
   AverageScore = MEAN(Score1 + Score2 + Score3 + Homework);
	  if AverageScore < 70;   
run;
A glance at the raw data shows that there should be students whose mean scores are below 70. However, the data set Work.Grades has 0 observations and 6 variables.
NOTE: The infile CLASS is:
      Filename=C:UsersMy SAS Files9.4class.dat,
      RECFM=V,LRECL=32767,File Size (bytes)=328,
      Last Modified=04Dec2017:11:01:52,
      Create Time=04Dec2017:10:55:21

NOTE: 4 records were read from the infile CLASS.
      The minimum record length was 80.
      The maximum record length was 80.
NOTE: The data set WORK.GRADES has 0 observations and 6 variables.
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=; 
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.
10243  data work.grades;
10244     infile class;
10245     input Name $1-7 Score1 Score2 Score3 Homework;
10246     Homework=Homework*2;
10247     AverageScore = MEAN(Score1 + Score2 + Score3 + Homework);
10248     PUTLOG Name= Score1= Score2= Score3= Homework= AverageScore=;
10249      IF AverageScore < 70;
10250  run;

NOTE: The infile CLASS is:
      Filename=C:UsersDocumentsMy SAS Files9.4class.dat,
      RECFM=V,LRECL=32767,File Size (bytes)=328,
      Last Modified=04Dec2017:11:01:01,
      Create Time=04Dec2017:10:55:21

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: 4 records were read from the infile CLASS.
      The minimum record length was 80.
      The maximum record length was 80.
NOTE: The data set WORK.GRADES has 0 observations and 6 variables.
Fix the error by replacing the plus signs in the MEAN function with commas. You can drop the PUTLOG statement and use a PROC PRINT statement to view your results.
data work.grades; 
   infile class; 
   input Name $1-7 Score1 Score2 Score3 Homework;
   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 4.4 Corrected Program Output
Corrected Program Output

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 errors:
  • 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
When the program below is submitted, the DATA step executes, but the PROC step does not. The PROC step does not execute because there is no following DATA or PROC step to indicate the beginning of a new step, nor is there a following RUN statement to indicate the end of the step.
data sasuser.admitfee; 
   set sasuser.admit; 
proc print data=sasuser.admitfee; 
   var id name actlevel fee;
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. But in the SAS windowing environment, because there is nothing to indicate the end of the PROC step, the PRINT procedure waits before executing, and a PROC PRINT running message appears at the top of the active window.
Note: Although omitting a RUN statement is not technically an error, it can produce unexpected results.

Correcting the Error: Missing RUN Statement

To correct the error, submit a RUN statement to complete 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 sasuser.admitfee; 
   set sasuser.admit; 
run;
proc print data=sasuser.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 4.2 SAS Log: Error Messages
9237  data sasuser.admitfee;
9238      set sasuser.admit;
9239  run;

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


9240  proc print data=sasuser.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 Log window 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 sasuser.admitfee; 
   set sasuser.admitfee; 
   where actlevel='HIGH;
run; 
proc print data=sasuser.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.
Figure 4.5 SAS Editor Window with Message
Editor with Message
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.
Tip
If you do not correct this error when it occurs in the SAS windowing environment, it is likely that any subsequent programs that you submit in the current SAS session will generate errors.

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 Operating 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 used with the PROC PRINT statement.
data sasuser.admitfee;
   set sasuser.admit;
   where weight>180 and (actlevel="MOD" or actlevel="LOW");
run;
proc print data=sasuser.admitfee keylabel; 
   label actlevel='Activity Level'; 
run;
When a SAS statement that contains an invalid option is submitted, a message appears in the Log window indicating that the option is not valid or not recognized.
Log 4.3 SAS Log: Syntax Error Message
9250  data sasuser.admitfee;
9251      set sasuser.admit;
9252      where weight>180 and (actlevel="MOD" or actlevel="LOW");
9253  run;

NOTE: There were 2 observations read from the data set SASUSER.ADMIT.
      WHERE (weight>180) and actlevel in ('LOW', 'MOD');
NOTE: The data set SASUSER.ADMITFEE has 2 observations and 9 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


9254  proc print data=sasuser.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.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds

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 Log window again to make sure there are no other errors.
Last updated: January 10, 2018
..................Content has been hidden....................

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