Announcement

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

  • sum values accross rows only if none missing

    Hello, I am trying to generate a new variable equal to the sum for a number of observations only if none of the observations are missing (see data below) and equal to missing if any observation is missing. I cannot use a simple STATA command such as sum or total as these take missings as zero. Any help on this is very much appreciated.

    I need to do this after using mi impute, so should hopefully something that can be use with mi passive.

    Thank you very much in advance.
    Rodolfo.
    StudyNo Var1 NewVariable
    1 0 .
    1 1 .
    1 1 .
    1 2 .
    1 1 .
    1 . .
    2 0 .
    2 2 .
    2 2 .
    2 . .
    3 0 16
    3 1 16
    3 2 16
    3 1 16
    3 3 16
    3 4 16
    3 5 16
    4 0 .
    4 0.07 .
    4 0.01 .
    4 0.01 .
    4 0.03 .
    4 0.02 .
    4 0.05 .
    4 . .

  • #2
    Code:
    .egen nmissing = total(missing(var1)), by(studyno)
    
    .egen wanted = total(var1) if nmissing == 0, by(studeyno)

    Comment


    • #3
      Thank you Nick for your answer. The code does work with the simplified data example. However, it does not seem to work with the mi dataset. Below is an extract of the dataset for the first individual.
      _mi_m = 0 denotes the original data with missing values for var1 (the variable I want to sum up). _mi_m >0 denote the imputed data datasets and these have complete data. However, nmissing equals 1 in all but one observation. I was expecting a zero for all observation for mi_m >0. Am I missing something?

      I used
      . mi passive: egen nmissing = total(missing(var1)), by(StudyNo )

      and using
      . mi passive: egen nmissing = total(missing(var1)), by( _mi_m StudyNo ) // would give the same results.

      Thank you very much again for your help.
      Rodolfo.

      StudyNo _mi_id _mi_miss _mi_m temp var1 nmissing
      1 371 0 0 1 0 1
      1 755 0 0 2 0.0304656 1
      1 2548 0 0 3 0.0150765 1
      1 2851 0 0 4 0.0172303 1
      1 3318 0 0 5 0.0161954 1
      1 3279 0 0 6 0.0188946 1
      1 3381 0 0 7 0.0269923 1
      1 2573 0 0 8 0.0188946 1
      1 3751 0 0 9 0.0620822 1
      1 2377 0 0 10 0.0080977 1
      1 3177 0 0 11 0.0215938 1
      1 5540 0 0 12 0.0512853 1
      1 6082 0 0 13 0.0377892 1
      1 6824 1 0 14 . 1
      1 371 . 1 1 0 1
      1 755 . 1 2 0.0304656 1
      1 2548 . 1 3 0.0150765 1
      1 2851 . 1 4 0.0172303 1
      1 3318 . 1 5 0.0161954 1
      1 3279 . 1 6 0.0188946 1
      1 3381 . 1 7 0.0269923 1
      1 2573 . 1 8 0.0188946 1
      1 3751 . 1 9 0.0620822 1
      1 2377 . 1 10 0.0080977 1
      1 3177 . 1 11 0.0215938 1
      1 5540 . 1 12 0.0512853 1
      1 6082 . 1 13 0.0377892 1
      1 6824 . 1 14 0.6516068 0
      1 371 . 2 1 0 1
      1 755 . 2 2 0.0304656 1
      1 2548 . 2 3 0.0150765 1
      1 2851 . 2 4 0.0172303 1
      1 3318 . 2 5 0.0161954 1
      1 3279 . 2 6 0.0188946 1
      1 3381 . 2 7 0.0269923 1
      1 2573 . 2 8 0.0188946 1
      1 3751 . 2 9 0.0620822 1
      1 2377 . 2 10 0.0080977 1
      1 3177 . 2 11 0.0215938 1
      1 5540 . 2 12 0.0512853 1
      1 6082 . 2 13 0.0377892 1
      1 6824 . 2 14 0.6516068 0
      1 371 . 3 1 0 1
      1 755 . 3 2 0.0304656 1
      1 2548 . 3 3 0.0150765 1
      1 2851 . 3 4 0.0172303 1
      1 3318 . 3 5 0.0161954 1
      1 3279 . 3 6 0.0188946 1
      1 3381 . 3 7 0.0269923 1
      1 2573 . 3 8 0.0188946 1
      1 3751 . 3 9 0.0620822 1
      1 2377 . 3 10 0.0080977 1
      1 3177 . 3 11 0.0215938 1
      1 5540 . 3 12 0.0512853 1
      1 6082 . 3 13 0.0377892 1
      1 6824 . 3 14 0.5359771 0

      Comment


      • #4
        Surprisingly or not, I don't ever work with data imputation in this sense and so will leave #3 open for others. If you don't get a good answer within 24 hours, I suggest that you

        1. start a new thread mentioning imputation in the title to attract attention from people who use mi

        2. there cross-reference this thread

        3. post a cross-reference in this thread to the new thread.

        Alternatively, this question could be one for StataCorp technical services.
        Last edited by Nick Cox; 15 Feb 2023, 04:57.

        Comment


        • #5
          Just adding a link to the related thread mentioninikg multiple imputation. Thank you. Rodolfo.
          Hello, I am trying to generate a new variable equal to the sum for a number of observations only if none of the observations are missing. On another thread

          Comment

          Working...
          X