What's wrong with this code?

3

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.

Programming exercise

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

Solution

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:

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

Share

About Author

Susan Slaughter

Author

Susan Slaughter (left) is best known as one of the authors of The Little SAS Book. Her newest book, written with Rebecca Ottesen and Lora Delwiche, is Exercises and Projects for The Little SAS Book Fifth Edition. Susan discovered SAS software in graduate school over 30 years ago. Since then she has used SAS in a variety of business and academic settings. She has presented over 90 papers at local, regional, and international SAS user group conferences, and currently works as a consultant through her company, Avocet Solutions.

3 Comments

  1. Hi Susan,
    Your The Little SAS® Book is one of my favorites and I recommend it to everyone who learns SAS.
    This summer, my son, who just finished his freshman year at the UMD is learning SAS. I forwarded this post to him, let's see how he handles your "programming exercise" 🙂

    • The exercise went pretty smoothly--it took me about two minutes to fix this code. I don't want to spoil any of the bugs for other readers, so I'll speak in broad terms. I used SAS Studio and can confirm that the format code option really helped, as did the editor's color coding for uncovering unbalanced quotes, and examining the output data for inconsistencies.

  2. Changed: Content*s*, missing quote in format, BMI not specified in Tabulate, missing RUN boundaries, unneeded quit.

    title Height, Weight, and BMI;
    title2 by Sex and Age Group;

    proc contents data = SASHelp.Class;
    run;

    data BMI;
    set SASHelp.Class;
    Height_m = Height * 0.0254;
    Weight_kg = Weight * 0.4536;
    BMI = Weight_kg / Height_m**2;
    run;

    proc format;
    value $Sex 'M' = 'Boys'
    'F' = 'Girls';
    value AgeGp 11-12 = 'Preteens'
    13-16 = 'Teens';
    run;

    proc tabulate data=BMI;
    class Sex Age;
    var Height_m Weight_kg BMI;
    tables (Height_m Weight_kg BMI)*MEAN, Sex Age ALL;
    format Sex $Sex. Age AgeGp.;
    run;

Leave A Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top