Announcement

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

  • Replacing values in a variable when group observations are missing and any one of the values by group equals a constant

    Using Stata 15.1/Win 10

    I am trying to replace missing values in variable "drop" below equal to 1 if any observations by group are missing for drop and one of the values in drop by group is 0. The way that the dataset is constructed is such that there will be only one value of 0 for each group (i.e. keeping one and only one observation by group). I'd like drop to take a value of 0 when all remaining within group values of drop are not missing and each equals 1.

    The variable descriptions:
    group: by group identifer
    pid_ord: within group rank
    count: within group N
    latest: 1 when pid_ord == N by group, 0 otherwise
    drop: 1 tags for drop, 0 tags for keep. These values were generated based on other arguments, but I am only interested in populating the missing values that meet the conditions above.

    For example, no observation in group 1 would be replaced (no missing); the missing observation in group 2 would be 0 becuase there is only one observation left that is not 1; group 4 missings would be replaced with 1 since an observation for drop is 0, and so on. Thanks in advance for your help!

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(group pid_ord count latest drop)
    1 1 2 0 1
    1 2 2 1 0
    2 1 2 0 1
    2 2 2 1 .
    3 1 2 0 1
    3 2 2 1 0
    4 1 6 0 1
    4 2 6 0 .
    4 3 6 0 .
    4 4 6 0 .
    4 5 6 0 .
    4 6 6 1 0
    5 1 3 0 1
    5 2 3 0 .
    5 3 3 1 0
    6 1 3 0 1
    6 2 3 0 .
    6 3 3 1 0
    7 1 2 0 1
    7 2 2 1 0
    end

  • #2
    Your description is maybe too elaborate, but I think that this is what you want

    Code:
    bys group: egen drop0= max(inlist(drop, 0))
    replace drop = cond(drop0& missing(drop), 1, 0) if missing(drop)

    Comment


    • #3
      Your description is still unclear for the case wherein all the drops are missing (within a group). In such case (if any), you still wanna replace as 1, or as 0, or just keep them as missings? Below codes are other choices for you beside the solution of Andrew.

      * If you want 1s for such cases (same output with Andrew's solution)
      Code:
      bys group (drop): replace drop = drop[1] !=1 if drop ==.
      * If you want 0s for such cases
      Code:
      bys group (drop): replace drop = drop[1] ==0 if drop ==.
      * If you still keep them as missings
      Code:
      bys group (drop): replace drop = 1 - drop[1] if drop ==.
      For your example, where no cases of all missing drops (for a group) are found, the output of all solutions are the same.
      Last edited by Romalpa Akzo; 25 Sep 2018, 08:22.

      Comment


      • #4
        Andrew Musau Thank you, this was what I was looking for.

        Comment

        Working...
        X