dimanche 21 juin 2015

SAS Nested If Statements Producing Incorrect Result

ID no. 6 returns 'Underweight' when it should return 'Overweight'. The lines of code in question, lines 30-53, contain nested if statements and produce a single undesirable result. The purpose of these lines is to partition the sample into classifications of weight class: Underweight, Average or Overweight.

TITLE 'A SHORT SAS PROGRAM';
OPTIONS LS=72;
* Create data file with height and weight data;

DATA HTWT;
    INPUT ID GENDER $ HEIGHT WEIGHT;
    DATALINES;
1 M 68.5 155.0
2 F 61.2 99.00
3 F 63.0 115.0
4 M 70.0 205.0
5 M 68.6 170.0
6 F 65.1 125.0
7 M 72.4 220.0
;
    * Create a new categorical variable for height;

DATA HTWT;
    SET work.HTWT;

    IF HEIGHT < 68 THEN
        STATURE='Short';

    IF HEIGHT >=68 THEN
        STATURE='Tall';
RUN;

* Create a new categorical variable for weight;

DATA HTWT;
    SET work.HTWT;

    IF GENDER='M' THEN
        IF WEIGHT > 170 THEN
            WEIGHT_CLASS='Overweight';

        IF 170 >=WEIGHT >=150 THEN
            WEIGHT_CLASS='Average';

        IF WEIGHT < 140 THEN
            WEIGHT_CLASS='Underweight';

        ELSE IF GENDER='F' THEN
            IF WEIGHT > 120 THEN
                WEIGHT_CLASS='Overweight';

            IF 120 >=WEIGHT >=100 THEN
                WEIGHT_CLASS='Average';

            IF WEIGHT < 100 THEN
                WEIGHT_CLASS='Underweight';
        RUN;

        * Changing units of height from inches to centimeters;

        DATA HTWT;
            SET work.HTWT;
            HEIGHT=2.54 * HEIGHT;
        RUN;

        * Creates HEALTH_INDEX;

        DATA HTWT;
            SET work.HTWT;
            HEALTH_INDEX=WEIGHT/HEIGHT;
        RUN;

        * Print the data file HTWT;

        PROC PRINT DATA=HTWT;
            TITLE 'HEIGHT AND WEIGHT DATA';
        RUN;

        * Sorts the data by gender. Some procedures require sorted data;

        PROC SORT DATA=HTWT OUT=sorted;
            BY GENDER;
        RUN;

        * Print the sorted data file;

        PROC PRINT DATA=sorted;
            TITLE 'GENDER SORTED HTWT DATA';
        RUN;   

Aucun commentaire:

Enregistrer un commentaire