Announcement

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

  • Creating new variables by dividing two others that include zero

    Hi,

    I have the following table, which is a little output from the data that I have.

    I would like to create a new variable (new_var_5) by dividing var_1 and var_2 and also create another new variable (new_var_6) by dividing var_3 and var_4. I'm doing this so I'll be able to run different t-tests afterwards comparing the mean of new_var_5 and new_var_6. I know how to create new variables by dividing the two others, but the problem is that there are several zero-values in the columns that I want to divide resulting in a lot of missing data.

    Therefore, my question is - how would you deal with this, so I'll be able to compare the means of new_var_5 and new_var_6 without missing a lot of data that is still relevant for my research-question?

    var_1 var_2 var_3 var_4 new_var_5 new_var_6
    2 4 2 2 0.5 1
    5 10 5 20 0.5 0.25
    0 10 0 5 . .
    10 10 0 2 1 .
    Thank you very much!

    Regards Nanna



  • #2
    Could you double check your math? 0 divided by 10 is equal to 0, not undefined, so the result should not be missing:

    Code:
    clear
    input v1 v2 v3 v4
    2 4 2 2
    5 10 5 20
    0 10 0 5
    10 10 0 2
    end
    
    gen v5 = v1/v2
    gen v6 = v3/v4
    
    list
    Results:
    Code:
         +------------------------------+
         | v1   v2   v3   v4   v5    v6 |
         |------------------------------|
      1. |  2    4    2    2   .5     1 |
      2. |  5   10    5   20   .5   .25 |
      3. |  0   10    0    5    0     0 |
      4. | 10   10    0    2    1     0 |
         +------------------------------+

    Comment


    • #3
      Thank you vey much for your answer.

      You are of course right, I'm sorry. However, I also meant it the opposite way around. Sometimes var_2 is 0 and var_1 is 10. This will give a missing value.

      var_1 var_2 var_3 var_4 new_var_5 new_var_6
      2 4 2 2 0.5 1
      5 10 5 20 0.5 0.25
      10 0 0 5 . 0
      10 10 2 0 1 .

      Comment

      Working...
      X