Search code examples
sassas-ds2

In SAS DS2, how to create a simple program to calculate bmi


I am trying to learn some basic DS2 programming by writing a program that calculates BMI. I have written a program but I am getting 'ERROR: Line 47: Attempt to obtain a value from a void expression.'. What am I doing wrong?

Here is my program:

    proc ds2;
    data _null_;
    dcl double bmi;

    method bmi_calc(double height, double weight);
        dcl double bmi;
        bmi = weight/(height * height);
    end;

    method init();
      weight = 70.5;
      height = 1.68;
    end;

    method run();
        bmi = bmi_calc(height, weight);
        put 'BMI IS: ' bmi;
    end;

    method term();
        put bmi;
    end;

    enddata;
    run;
   quit;

Solution

  • You need to do two things with custom methods in ds2:

    1. Declare the type of value that you are going to return
    2. Return the value

    For example, this method returns the value 10.

    method foo() returns double;
        return 10;
    end;
    

    To make your method work, you simply need to state what type of variable you are returning and then return that value.

    method bmi_calc(double height, double weight) returns double;
        dcl double bmi;
        bmi = weight/(height * height);
        return bmi;
    end;