1 / 27

BMTRY 789 Lecture 11: Debugging

BMTRY 789 Lecture 11: Debugging. Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations. Debugging?. “If debugging is the process of removing bugs, then programming must be the process of putting them in.”

sanaa
Download Presentation

BMTRY 789 Lecture 11: Debugging

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. BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations

  2. Debugging? “If debugging is the process of removing bugs, then programming must be the process of putting them in.” –From some strange, but insightful website BMTRY 789 Introduction to SAS Programming

  3. Syntactic Errors vs. Logic Errors • This lecture focuses only on syntax errors; however, it is also possible for SAS to calculate a new variable using syntactically correct code that results in inaccurate calculations, i.e. a logic error. • For this reason, it is always wise to check values of a new variable against values of the original variable used in the calculation. BMTRY 789 Introduction to SAS Programming

  4. Testing • Test each part of your program separately before putting it all together • Use Proc Print after your data step to check if your data is correct • Use small data sets to initially test, or use portions of your data • Infile ‘Mydata.Dat’ OBS=100; • Infile ‘Mydata.Dat’ Firstobs=101 Obs=200; • Data mydata; Set olddata (Obs=50); BMTRY 789 Introduction to SAS Programming

  5. Wrong results…but no error? • This means that you have a logic problem not a syntax problem. SAS won’t catch these, you have to! • To figure out exactly what is happening in the data step use a PUT statement. SAS will then write the data to the Log PUT _all_; (use this for all variables and their values) PUT varname= varname = ; (just the variables that you select will be printed along with their values. If you don’t put the = then the variable names will not be printed, only the values) BMTRY 789 Introduction to SAS Programming

  6. READ THE LOG WINDOW!! • I know that I spout this all of the time, and that is because too many people begin skipping this step and then can’t figure out why their program isn’t working • If you have an ERROR message, look at that line as well as a few of the lines above it • Don’t ignore Warnings and Notes in the log simply because your program seems to have run, they could indicate a serious error that just did not happen to be syntactically incorrect, in this case, check your logic or add some Proc Prints to understand what is going on inside your program BMTRY 789 Introduction to SAS Programming

  7. Types of errors in the Log • There are 3 main types of messages that SAS will generate in the log: • Notes • Errors • Warnings BMTRY 789 Introduction to SAS Programming

  8. 1. "NOTE" • Notes are always generated in the log; they provide important information about the processing of the SAS program such as: • number of observations and number of variables in a newly created data set. • length of time taken by the processing (both real and cpu time). • indications of certain types of programming errors. BMTRY 789 Introduction to SAS Programming

  9. "NOTE" BMTRY 789 Introduction to SAS Programming

  10. Note: Numeric Values Have Been Converted to Character • When you accidentally mix numeric and character variable, SAS tries to fix your program by converting variables for you • Don’t let SAS do this, if you need a conversion done, do it yourself using the PUT() or INPUT() function. BMTRY 789 Introduction to SAS Programming

  11. 2. "ERROR" • Error messages are the most obvious clue that something is wrong with the SAS program. • Unlike Notes and Warnings, the program will not complete processing until the necessary changes have been made. • Because one error can result in multiple error messages, fixing the first-occurring error will frequently clear up the remaining error messages. BMTRY 789 Introduction to SAS Programming

  12. "ERROR" BMTRY 789 Introduction to SAS Programming

  13. 3. "WARNING" • Warnings are frequently indications of problems in how SAS processed the program (although not always, and it should be noted that SAS may not always stop processing the program). Warnings should always be investigated. BMTRY 789 Introduction to SAS Programming

  14. "WARNING" BMTRY 789 Introduction to SAS Programming

  15. Debugging: The Basics The better you can read and understand your program, the easier it is to find the problem(s). • Put only one SAS statement on a line • Use indentions to show the different parts of the program within DATA and PROC steps • Use comment statements GENEROUSLY to document your code BMTRY 789 Introduction to SAS Programming

  16. Know your colors • Make sure that you are using the enhanced editor and know what code is generally what color (i.e. comments are green) BMTRY 789 Introduction to SAS Programming

  17. Scroll Up • Remember that your output and log windows are scrolled to the very bottom of the screen, scroll ALL the way up and check the whole thing. • Look for common mistakes first (Semicolons and spelling errors!) • Make sure you haven’t typed an ‘O’ where you want an ‘0’ or vice versa, this can cause SAS to think that your numeric or character variable should be change to the other variable type. SAS may do this automatically when you don’t want it done! BMTRY 789 Introduction to SAS Programming

  18. What is wrong here? *Read the data file ToadJump.dat using a list input Data toads; Infile ‘c:MyRawData\ToadJump.dat’; Input ToadName$ Weight Jump1 Jump2 Jump3; Run; BMTRY 789 Introduction to SAS Programming

  19. Here is the log window… ___________________________________________________________ *Read the data file ToadJump.dat using the list input Data toads; Infile ‘c:\MyRawData\ToadJump.dat’; ------ 180 ERROR 180-322: Statement is not valid or it is used out of proper order. Input ToadName$ Weight Jump1 Jump2 Jump3; ------- 180 ERROR 180-322: Statement is not valid or it is used out of proper order. Run; __________________________________________________________ 1 2 3 4 5 BMTRY 789 Introduction to SAS Programming

  20. DATASTMTCHK System Option • Can make some mistakes easier to find, like the missing semicolon in the previous slide. • This prevents you from accidentally overwriting an existing data set just because you forget a semicolon. • You can make ALL SAS keywords invalid SAS data set names by setting this option to ALLKEYWORDS • Options DataStmtChk=Allkeywords; BMTRY 789 Introduction to SAS Programming

  21. Log window with this option 1 2 3 4 5 6 OPTIONS DATASTMTCHK=ALLKEYWORDS; *Read the data file ToadJump.dat using the list input Data toads Infile ‘c:\MyRawData\ToadJump.dat’; ------ 57 ERROR 57-185: Infile is not allowed in the DATA statement when option DATASTMTCHK=ALLKEYWORDS. Check for a missing semicolon in the DATA statement, or use DATASTMTCHK=NONE. Input ToadName$ Weight Jump1 Jump2 Jump3; Run; ERROR: Memtype field is invalid. BMTRY 789 Introduction to SAS Programming

  22. What’s wrong? BMTRY 789 Introduction to SAS Programming

  23. Long quote warning • A very common Warning is the one illustrated in the previous slide, saying that a quoted string has become extremely long. • Most frequently, the problem is a quote being inadvertently left out. • In this case, adding the missing quote (i.e., '820  '<=diag02<='82099') will fix the problem and remove the Error messages showing up in the rest of the program. BMTRY 789 Introduction to SAS Programming

  24. What else was wrong? BMTRY 789 Introduction to SAS Programming

  25. SAS is still running… • This will not, however, fix an associated problem. • A most important caveat when receiving this type of log message is to also check the message above the menu on the Log window. • If it says, as in this example, "DATA STEP running", then steps must be taken to stop the program from running. • Even though SAS will continue to process other programs, results of such programs may be inaccurate, without any indication of syntax problems showing up in the log. BMTRY 789 Introduction to SAS Programming

  26. SAS is still running… • Several suggestions to stop the program are: • Submit the following line: '; run; • Submit the following line: *))%*'''))*/; • If all else fails, exit SAS entirely (making sure that the revised program has been saved) and re-start it again. BMTRY 789 Introduction to SAS Programming

  27. SAS stops in the middle of a job • You have an unmatched quote or comment • You have no RUN statement at the end of your program As in the slide previous you may have to submit this in order to stop it from being stuck: *’; *”; */; Run; BMTRY 789 Introduction to SAS Programming

More Related