Announcement

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

  • Adding and subtracting variables in a row when some of the variables contain missing values


    The 10 observations shown in my dataex are taken from my dataset

    The variables X1, X2, X3, X4, and X5 are the different variables used to calculate the variable “value”

    The formula for calculating a value = X1 + X2 - X3 - X4 + X5



    I needed to calculate this value using the above formula

    I used the gen function as follows



    gen Calucated_value = X1 + X2 - X3 - X4 + X5



    Click image for larger version

Name:	dataex.PNG
Views:	1
Size:	110.8 KB
ID:	1692728







    As you can see in my file above, whenever I have missing values in one of my variables, the generated calculated value that is shown in the variable “Calculated value” is not equal to my “value” variable. In other words, whenever I have missing values in one of my variables, the calculated value is missing. I don’t want this to happen, unless all the variables X1, X2, X3, X4, and X5 are missing.



    Would you please let me know how to address this issue?

    Thank you very much, and looking forward to your advice



  • #2
    Your dataex is intended to be helpful but rendered as a screenshot it is much less helpful than you hope. See advice against screenshots in the FAQ.

    If what you want is to treat missings as zeros (which isn't stated) then you could approach the problem like this:


    Code:
    clear 
    set obs 3 
    set seed 314159265
    
    forval j = 1/3 {
        gen X`j' = cond(runiform() < 0.3, ., runiformint(1,5)) 
    }
    
    
    gen Calculated = cond(missing(X1), 0, X1) + cond(missing(X2), 0, X2) - cond(missing(X3), 0, X3) 
    
    list 
    
         +-------------------------+
         | X1   X2   X3   Calcul~d |
         |-------------------------|
      1. |  5    5    4          6 |
      2. |  .    1    .          1 |
      3. |  1    2    3          0 |
         +-------------------------+
    Alternatively see

    Code:
    help mvencode

    Comment


    • #3
      Thank you very much Nick Cox.
      Ok, I will look at advice against screenshots in the FAQ.

      I have some issues with performing forval. In my main dataset, the variable names are not just X1, X2, X3, X4, and X5. I used these names to explain my question easily.
      The real names are:
      X1= total_payment
      X2 = support
      X3= fee
      X4= dedication
      X5 = post_payment

      Would you please give me advice on how to write the for value command above which I copy and paste again below, with my real variable names?




      forval j = 1/3 {
      gen X`j' = cond(runiform() < 0.3, ., runiformint(1,5))
      }



      Thank you again and looking forward to hearing from you,
      Last edited by tig som; 09 Dec 2022, 07:58.

      Comment


      • #4
        I used a loop to set up for myself a data example. I can't see, however, that a loop would help you as you have a mix of + and -. The code is a little tedious but starts with just

        Code:
         
         cond(missing(total_payment), 0, total_payment)

        Comment


        • #5
          It worked perfectly. Thank you very much, Nick Cox!!

          Comment

          Working...
          X