1 / 14

Chapter 3: Editing and Debugging SAS Programs

Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor. Add line number: In the Command Box, type num, enter.

saki
Télécharger la présentation

Chapter 3: Editing and Debugging SAS Programs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3: Editing and Debugging SAS Programs

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

  3. What does SAS System do when we submit a program? When we submit a SAS program, SAS performs the following tasks: • Compile the program to check for syntax errors. • If there is no syntax errors, execute the program to read, process, manipulate the data. • 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.

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

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

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

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

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

  9. 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) : • Press Ctrl and Break keys or click the Break Icon (the acclamation in circle, next to the RUN icon) on the tool bar. • Select Cancel Submitted Statements, then click OK, • Select Y to cancel submitted statements, OK. • Correct the error, resubmit the program.

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

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

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

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

  14. Exercise Open c2_sysoptions from your SASEx folder Create some common errors such as: • missing semicolon • Wrong key word • Missing quotation mark • Missing RUN; statement One by one and observe the error messages in the SAS log.

More Related