Announcement

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

  • How Can I Remove Some Data Points Correctly in Stata?

    I have a small dataset as follows,
    clear
    input sid year sign
    1 1 0
    1 2 0
    1 3 0
    1 4 0
    1 5 0
    1 6 0
    1 7 0
    2 1 1
    2 2 0
    2 3 0
    2 4 0
    2 5 0
    2 6 0
    2 7 0
    3 1 1
    3 2 1
    3 3 1
    3 4 1
    3 5 1
    3 6 1
    3 7 1
    end

    I want to remove the observations if all the values of sign variable are 0 within group (sid).
    In other words, I want to keep the data if year==1 & sign==1
    Could you help me with Stata code?
    Thank you!
    Last edited by smith Jason; 15 Jul 2022, 13:44.

  • #2
    What about this?

    Code:
    keep if year==1 & sign==1

    Comment


    • #3
      Thank you. It is not what I want. What I want is to remove all the observations when all the values of sign variable are 0 within sid.

      Comment


      • #4
        bys sid (year): keep if [1] ==1 & sign [1] ==1
        Thank you, folks! I come up with it finally!

        Comment


        • #5
          Okay, it is not clear what you mean by observations "within" sid. All observations are within sid in the sense that all observations have some value associated with sid. it sounds kinda like you want this:

          Code:
          bysort sid: drop if sign == 0
          But assuming your data is already sorted by sid, the above is exactly equivalent to this:

          Code:
          drop if sign == 0
          Unless what you actually want is your data sorted by sid then the drop command in which case you want this:

          Code:
          sort sid
          drop if sign == 0
          In any case, by isn't doing anything for you here. If none of these work, then I need a more detailed, precise description of the behavior you are looking for.

          Edit: regarding #4, this is not functionally different than the second line of code in this post.
          Last edited by Daniel Schaefer; 15 Jul 2022, 14:19.

          Comment


          • #6
            Thank you for your response. However, your code is not what I want.

            Comment


            • #7
              Okay. I'm glad you've found a solution in #4 that works.

              Comment

              Working...
              X