Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calculating BMI when ht and wt have been recorded with different units

    Hello,

    I am trying to create a new variable for BMI and I have a dataset with continuous variable "height" and another variable "htunits" recorded as cm or inches, and continuous variable "weight" with variable "wtunits" recorded as kg or lbs. When I attempt to convert all height values to m and weight to kg using the gen and replace commands I am getting an error "type mismatch"

    Could anyone suggest syntax to do this properly?

    Thanks in advance

    Rene

  • #2
    You are likely referring to a string variable as if it were numeric. Any string variable has to have quotes when its value is referenced, e.g.

    Code:
    gen height_inches = height * 2.54 if htunits == "inches"
    Or perhaps vice versa. If you show your code (use the code delimiters like I did, see my signature), we can help you further.
    Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

    When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

    Comment


    • #3
      I checked and the variables height, weight, htunit and wtunit are storage type "double", meaning the unit variables are incorrectly stored as numerical

      When I use the following, as you suggested
      Code:
      gen weight_kg= weight/2.205 if wtunit=="lbs"
      I get a type mismatch error. Could you explain 1) how I would convert my unit variables into the proper format to use the code? and 2) what command would I use to carry over the weight data that is inputted in kg already?

      Here is a sample of my data

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input double(weight wtunit height htunit)
       57 1 157 1
       50 1  61 2
       64 1 163 1
       75 1 170 1
       74 1 165 1
       93 1 175 1
      107 1 189 1
      143 1 185 1
      132 2  60 2
      120 1 183 1
       66 1 162 1
       59 1 164 1
       93 1 173 1
       78 1 174 1
      119 2  60 2
       72 1 168 1
       43 1 147 1
       95 1 175 1
       80 1 167 1
      109 1 169 1
       70 1 175 1
       86 1  72 2
       61 1 168 1
      160 2  67 2
      111 1 178 1
      114 1 155 1
       88 1 177 1
       57 1 150 1
       87 1 178 1
       89 1 160 1
       57 1 157 1
       75 1 182 1
       99 1 182 1
       48 1 161 1
       77 1 165 1
       77 1 170 1
       59 1 165 1
      146 2 155 1
       65 1 170 1
       66 1 167 1
      110 1 178 1
       91 1 168 1
      130 2  62 2
       64 1 160 1
       92 1 185 1
       43 1 157 1
       58 1 149 1
       85 1 171 1
      102 1 187 1
      114 1 176 1
       82 1 180 1
       93 1 175 1
       94 1 177 1
       90 1 167 1
       87 1 165 1
       77 1 169 1
       91 1 171 1
       45 1 157 1
       70 1 178 1
       75 1 172 1
       50 1 156 1
       67 1 160 1
       63 1 172 1
       94 1 172 1
       75 1 162 1
      106 1 168 1
       62 1 157 1
      220 2  68 2
       96 1 170 1
      150 2  69 2
       82 1 152 1
       70 1 178 1
       75 1 158 1
       67 1 158 1
       75 1 155 1
      109 1 165 1
       64 1 170 1
       92 1 183 1
       63 1 157 1
       79 1 180 1
       88 1 173 1
       90 1 183 1
       80 1 160 1
       75 1 156 1
      102 1 170 1
       89 1 190 1
       73 1 165 1
       48 1 150 1
       96 1 162 1
       68 1 170 1
       93 1 170 1
       84 1 175 1
      162 2  64 2
       99 1 180 1
       45 1 155 1
       93 1 172 1
       74 1 168 1
       64 1 150 1
       59 1 157 1
       53 1 168 1
      end
      label values wtunit wtunit
      label def wtunit 1 "kg", modify
      label def wtunit 2 "lbs", modify
      label values htunit htunit
      label def htunit 1 "cm", modify
      label def htunit 2 "in", modify

      Comment


      • #4
        I gather you’ll need to - generate - and - replace- two variables (say, height_m and weight_kg) under the ‘if’ clause. After that, you may proceed with the generation of the BMI variable.
        Best regards,

        Marcos

        Comment


        • #5
          Thank you Marcos! I was incorrectly using the label in my code rather than the number 1 or 2. It works now

          Rene

          Comment


          • #6
            Code:
            gen BMI = cond(wtunit == 1, weight, weight * 0.453592) / (cond(htunit == 1, height/100, 2.54 * height/100)^2)

            Comment


            • #7
              I have a related question. How would you convert a variable that is coded as "stones and pounds" to kilograms?
              Look forward to hearing from you.
              Best wishes,
              Laurita

              Comment


              • #8
                Please present data as shown in #3. Since it is your first post, please read the FAQ, particularly the topics about sharing data/command/output. You may wish to start a new thread as well.
                Best regards,

                Marcos

                Comment

                Working...
                X