Chapter 3: Editing and Debugging SAS Programs Some useful tips of using Program Editor • Add line number: In the Command Box, type num, enter. • Save SAS program: For a new program, go to File, SAVE As , go to the folder in which you will save the program, give a name, and save it. For a saved program, to update the it, go to File, SAVE. • Clearing SAS program, SAS LOG or OUTPUT Window, activate the window, right-click, Edit, Clear All (Or go to Edit, Clear All). Text that has been cleared can not be Undo. However, The program in the Program Editor can be Undo: Edit, Undo to recall the deleted program. What does SAS System do when we submit a program? When we submit a SAS program, SAS performs the following tasks: (1) Compile the program to check for syntax errors. (2) If there is no syntax errors, execute the program to read, process, manipulate the data. (3) Analyze the data and generate the resulting reports. At the step (1), results of compiling the program are displayed in the SAS Log window. The programmer checks the SAS Log to correct the program and resubmit it until no syntax error. At Step (2), read, process and manipulate the data. If the program logic is not correct, the results will not be correct. Some errors may be displayed in the SAS Log, but some errors may not be displayed. These errors are not due to syntax or due to data, but, due to the program logic is not correct to accomplish the purpose. may not be displayed as all. At Step (3), data are analyzed. Errors occur often due to wrong statistical analysis. This requires proper statistical knowledge to correct the errors. Error Messages in SAS Log In general, SAS Log displays two types of errors: • Syntax errors: due to incorrect use of program statements. Statements do not confirm the rules of SAS language. Common errors are misspell words, omitting semicolons, leaving quotation marks unbalanced, specifying invalid options, misuse rules of statements. • Data errors: some data are not appropriate for SAS statements as written in the program. For example, wrong data type, wrong formats used in the SAS statement to read data, inappropriate processing of the data, and so on. Identifying errors and resolving problems In general, SAS uses color-coding system to display right (in BLUE) and wrong (in red) errors in SAS log. After submitting the program, go to SAS Log to check if there are ‘red’ error messages. Indentify the errors, go back to SAS program, correct the error statements, resubmit the program, repeat the process until no error message in SAS Log. For common errors such as misspelling word, omitting semicolon, violating rules of statements, simply correct the error in the program. SAS Log provides some detail error messages regarding to each error. However, these error messages may not provide a direct information about the error, since SAS system diagnoses and displays the errors due to the consequence of the mistake, not the mistake itself. Resolving some common problems Missing RUN statement: There are two steps in SAS program: Data Step and PROC step. Each step ends with a RUN statement. SAS recognizes the end of the current step when it encounters • a new DATA or PROC step • A RUN or QUIT statement indicating the end of the step. Usually, there is no error message even you omit RUN statement , if there is a new DATA or PROC step following the current step. If none of the above two situations, the last step will not be executed. Ex: Data fee; set admitfee; Proc print data = fee; In the program, Data step is properly executed. But, PROC print is not executed. To resolve this: submit a RUN; statement to complete the PROC step: Run; Missing Semicolon: SAS does not stop at the statement, instead, it continue to check the next statement and assumes it is part of the current statement until a semicolon is found. Error message is about the syntax errors due to the mixed up of more than one statements. Therefore, the errors do not tell you that you miss a semicolon. Typical error message is about the first word of the next statement followed right after the missing semicolon. To resolve the problem, add semicolon to the statement, and resubmit the program. Unbalanced Quotation Marks: SAS can not identify the missing quotation mark. It continues to read the text next to the missing quotation and looks for the next mark. Error messages can be totally unexpected. A few common messages may be • A quoted string has become too long. • A statement that contains quotation mark is ambiguous due to invalid options or unquoted text. To resolve the problem: NOTE: By adding the quotation mark to the quoted string DOES NOT solve the problem. Two ways to resolve unbalanced mark problem One way to resolve the problem is to cancel the submitted program, correct the program, then submit the correct program (in Window or Unix system) : 1. Press Ctrl and Break keys or click the Break Icon (the acclamation in circle, next to the RUN icon) on the tool bar. 2. Select Cancel Submitted Statements, then click OK, 3. Select Y to cancel submitted statements, OK. 4. Correct the error, resubmit the program. Invalid Options: SAS can, in general, recognize the problem and displays the error message in SAS Log: • The option or parameter is not recognized. To resolve it, correct the program, and resubmit it. COMENT STATEMENTS in SAS program: Any statements inside /* and */ are comments. They are not SAS program statement. Not to be executed. Any statement starts with * is also a comment statement. Use SAS System Options to handle error messages: OPTIONS ERROR=n : specify maximum # of observations for which the data error message are printed. Error Checking in Enhanced or Program Editor: SAS uses color coding to display key words, trivial syntax errors in both Enhanced and Program editor when you are writing program statements. Enhanced Editor has more color-codes than Program Editor. Both use BLUE for Key words, Green for correct options or quoted strings. If there is a syntax error, you will see colors such as red, purple. After a while, you will be able to identify the errors immediately using these color codes and correct statements with trivial syntax errors. Debugging with Data Step Debugger Syntax errors are easier to detect and correct. Programs is not ready for processing data if there are syntax errors. However, once the program is free of syntax error, it does not mean the program is logically correct for processing and analyzing data. NOTE: Logic errors do not stop a program from running. In stead, they cause the program to produce unexpected results. To debug the logic errors is a critical process in order to produce correct results. The process of removing logic errors from a program is ‘DEBUGGING’. Use SAS option: DEBUG in the Data Step to debug logic error. DATA mylib.budget / debug; Input name $ 1-10 gender $ 12 age 15-17 income 20-30; Run; Proc print; Run; NOTE: the key word after ‘/’ is an option of a SAS statement. In this case, it is an option of the Data Step. Debug option allows for issuing commands to execute DATA step statements one-by-one, pause, display the resulting variable values in the window. The detailed commands available for the DEBUGGER can be found in the SAS HELP Document. NOTE: This debug option is used only in Interactive Mode. To get out from the Interactive Model, enter QUIT in the Commend line, then, return. Points to Remember • It is a good idea to begin DATA steps and RUN statements on the left and indent statements within a step. • End each step with a RUN statement. • Review messages in the Log window each time after submitting a SAS program. • You can submit only the revised steps in the program for syntax checking. • Clear SAS Log window regularly for easier error checking during the program development. Exercise Open c2_sysoptions from your SASEx folder Create some common errors such as: 1. missing semicolon 2. Wrong key word 3. Missing quotation mark 4. Missing RUN; statement One by one and observe the error messages in the SAS log.