270 likes | 418 Views
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.”
E N D
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3rd 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.” –From some strange, but insightful website BMTRY 789 Introduction to SAS Programming
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
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
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
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
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
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
"NOTE" BMTRY 789 Introduction to SAS Programming
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
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
"ERROR" BMTRY 789 Introduction to SAS Programming
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
"WARNING" BMTRY 789 Introduction to SAS Programming
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
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
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
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
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
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
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
What’s wrong? BMTRY 789 Introduction to SAS Programming
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
What else was wrong? BMTRY 789 Introduction to SAS Programming
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
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
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