Announcement

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

  • summing and subtracting when there is missing

    Hello,
    I try to generate a variable that is includes missing, how can I ignore these missing values

    A002112000 A002113000 A002114000 A002115000 A0i2116000 A0i2117000
    7.851e+09 2.904e+10
    862729.14 -1401101.7 0 212157.58 0 0
    1142823 2853657.8 0 8015518.9 0 0
    2490640.9 7834515.8 0 3013278 0 0
    2170997.8 3045616 0 2813278 0 0
    2003422.3 1464629.9 2813278
    1995585.5 4733366.3 2813278


    I need to generate HH=A002112000 + A002113000 - A002114000 - A002115000 - A0i2116000 - A0i2117000

  • #2
    Code:
    help mvencode
    Or know that

    Code:
    cond(missing(x), 0,  x)
    treats missings in x as if they were zeros, so ignores them in addition and subtraction.

    Comment


    • #3
      Emad:
      the easiest approach that springs to my mind is to flag observations with missing value in any of the variables;
      1) change . in zero;
      2) do the calculation;
      3) trasform back to . when finished:
      Code:
      . egen flag=rowmiss( A002112000-A0i2117000)
      
      .  foreach var of varlist A002112000 - A0i2117000 {
        2.replace `var'=0 if `var'==.
        3.}
      
      . generate HH=A002112000 + A002113000 - A002114000 - A002115000 - A0i2116000 - A0i2117000 if flag>0
      
      .  foreach var of varlist A002112000 - A0i2117000 {
        2. replace `var'=. if `var'==0 & flag>0
        3. }
      
      
      .
      PS: tragically inefficient when contrasted against Nick's elegant solution!
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        I'd be reluctant to use such long variable names.

        Code:
        A002112000  A002113000 A002114000 A002115000  A0i2116000  A0i2117000
        Do you need the trailing 000 ? Is there scope for names that mean something to the researcher?

        Comment


        • #5
          I'd be reluctant to "ignore" missing values; if one or more values is missing, we naturally cannot know the result either.

          Comment


          • #6
            Originally posted by Nick Cox View Post
            Code:
            help mvencode
            Or know that

            Code:
            cond(missing(x), 0, x)
            treats missings in x as if they were zeros, so ignores them in addition and subtraction.
            Thanks very much, it is working

            Comment


            • #7
              Originally posted by Carlo Lazzaro View Post
              Emad:
              the easiest approach that springs to my mind is to flag observations with missing value in any of the variables;
              1) change . in zero;
              2) do the calculation;
              3) trasform back to . when finished:
              Code:
              . egen flag=rowmiss( A002112000-A0i2117000)
              
              . foreach var of varlist A002112000 - A0i2117000 {
              2.replace `var'=0 if `var'==.
              3.}
              
              . generate HH=A002112000 + A002113000 - A002114000 - A002115000 - A0i2116000 - A0i2117000 if flag>0
              
              . foreach var of varlist A002112000 - A0i2117000 {
              2. replace `var'=. if `var'==0 & flag>0
              3. }
              
              
              .
              PS: tragically inefficient when contrasted against Nick's elegant solution!
              Thanks very much. Yes it is long :D compared to the Nick's code

              Comment

              Working...
              X