Whether you enjoy debugging or hate it, for programmers, debugging is a fact of life. It’s easy to misspell a keyword, scramble your array subscripts, or (heaven forbid!) forget a semicolon. That’s why we include a chapter on debugging in The Little SAS® Book and its companion book, Exercises and Projects for the Little SAS® Book. We believe that learning to debug makes you a better programmer. Once you understand a bug, you will be better prepared to avoid it in the future.
To help hone your debugging skills, here is an example of the type of problems you can find in our book of exercises and projects. See if you can find the bugs.
- A friend tells you that she is learning SAS and wrote the following program. Unfortunately, the program won’t run. Help her improve her programming skills by finding the mistakes.
TITLE Height, Weight, and BMI; TITLE2 by Sex and Age Group; PROC CONTENT DATA = SASHELP.class; RUN; DATA; SET SASHELP.class; Height_m = Heigth * 0.0254; Weight_kg = Weight * 0.4536; BMI = Weight_kg / Height_m**2; PROC FORMAT; VALUE $sex 'M' = 'Boys' 'F' = 'Girls'; VALUE agegp 11-12 = 'Preteens 13-16 = 'Teens'; PROC TABULATE; CLASS Sex Age; VAR Height_m Weight_kg; TABLES (Height_m Weight_kg BMI)* MEAN, Sex Age ALL; FORMAT Sex $sex. Age agegp.; RUN; QUIT;
- a. Examine the SAS data set SASHELP.CLASS including variable attributes.
- b. Clean up the formatting of the program by adding appropriate indention and line spacing to show the structure of the DATA and PROC steps. Make changes as needed to make the program conform to standard best practices.
- c. Fix any errors in the code so that the program will run correctly.
- d. Add comments to the revised program for each bug that you fix so that your friend can understand her mistakes.
In the book, we provide solutions for odd-numbered multiple choice and short answer questions, and hints for the programming exercises. So here is a hint for this exercise:
- Hint: This program contains four bugs. It also contains “red herrings” that are unusual for SAS code, but nonetheless do run properly and so are not actual bugs. Be sure you know how SAS handles data set names by default. SAS Enterprise Guide can format code for you; right-click the Program window and select Format Code from the pop-up menu. To format code in SAS Studio, click the Format Code icon at the top of Program window.
For more about The Little SAS Book and its companion book of exercises and projects, check out these blogs:
- The Little SAS Book 6.0: The best-selling SAS book gets even better
- Expand your SAS Knowledge by Learning PROC SQL
- Test your SAS skills with the newest edition of Exercises and Projects for The Little SAS Book