Announcement

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

  • Tell Stata to ignore missing values when adding variables

    I want to create a new variable in Stata, that is a function of let's say 3 different variables, A, B and C, like so:
    gen new_var = ((A)/3) + ((B)/2) + ((C)/4)
    All observations have missing values for one or two of the variables, but that is not relevant to what I am trying to do. When I run this command, all it generates are missing values, because no observation has values for all 3 of the variables. I want STATA to complete the function and treat missing variables as 0 in the function. Is that possible with a simple command, without generating new dummy variables?
    Many thanks.

  • #2
    The -cond()- function seems to be the (or at least a) way to go (though I look forward to seeing what other users come up with):

    Code:
    gen x = cond(missing(a), 0, a/3) + cond(missing(b), 0, b/2) + cond(missing(c), 0, c/4)
    E.g.,

    Code:
    clear all
    input a b c
    9 10 8
    6 . 12
    3 . .
    end
    
    gen x = cond(missing(a), 0, a/3) + cond(missing(b), 0, b/2) + cond(missing(c), 0, c/4)
    
    li
    Last edited by Brendan Cox; 06 Oct 2015, 08:18.

    Comment


    • #3
      Brendan gives good advice. In addition, if missings are really to be regarded as zeros, then mvencode beckons.

      Comment


      • #4
        I want STATA to complete the function and treat missing variables as 0 in the function. Is that possible with a simple command, without generating new dummy variables?
        Just replace the missing values with 0 then. Like

        Code:
        recode A B C (missing = 0) , prefix(new_)
        I wonder, however, what the result might mean. Usually it is not a good idea to replace some unknown value (missing) with a value without accounting for the uncertainty associated with this change. I trust you know what you are doing.

        Best
        Daniel
        Last edited by daniel klein; 06 Oct 2015, 08:18.

        Comment


        • #5
          Thanks a lot, this worked really nicely, very useful!

          Comment


          • #6
            How do I generate var X that takes the row mean and ignores the missing?
            Last edited by Co Ar; 06 Jan 2017, 02:18.

            Comment


            • #7
              what if I need to gen var X that takes the row mean and ignores the missing?

              Comment


              • #8
                You may wish to type - help egen - and check the aptly named -rowmean - option.
                Best regards,

                Marcos

                Comment


                • #9
                  you can also try this:
                  use the egen commnand
                  and then use rowtotal, add all your variables, and then so a ,missing
                  such as:

                  egen WTP = rowtotal(FFWTP CFWTP ITWTP BWTP), missing

                  thanks.

                  C

                  Comment


                  • #10
                    Hi, iam using endogenous switching regression in a panel data setting,my concern is,could you help me with commands on the same,i am trying to read but i have not gotten the concept well.thanks

                    Comment


                    • #11
                      Francis: Your topic

                      Endogenous switching regression in a panel data setting

                      has nothing whatsoever to do with this thread, so you should consider starting in a new thread with a title like that. That said, posts of the form "I need to understand X" are less likely to be answered than concrete, specific questions showing code.

                      Comment


                      • #12
                        Hi,

                        I have a similar question. How can I apply excel COUNTA function in stata. In other words, how do I count all non empty cells in stata and generate a new variable that has the count of all non empty cells?

                        Comment


                        • #13
                          I wouldn't assume that experienced Stata users here know much about MS Excel. But perhaps type

                          Code:
                          help egen
                          for its count() function.

                          Comment


                          • #14
                            Thank you for the response. I'll look at the count () function

                            Comment


                            • #15
                              Dear Statalist

                              I have a total of 5600 observations for children who were able to get vaccinated (5000 yes, 600 no), however when generating the dummy for vaccines, it is capturing missing values for the entire dataset. I have a total of 7000 Wazs observations, as such the dummy is assuming 2000 no (i.e the 600 and the 1400 missing observations) . Is there a way to generate the dummy to only take into consideration the 5600 vaccinated observations?

                              gen Vacc =1 if Immu==1
                              replace Vacc=0 if mi(Vacc)
                              Thanks for your help.

                              Kind Regards!

                              Comment

                              Working...
                              X