Announcement

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

  • Retaining the entire ID if there are certain variables in the panel data

    Good Morning world

    English is not my language so google help me. Please understand me.

    I have stata 15.

    I`m using panel data with 191 variables and this data has been investigated for about 22years

    ID goes from 101 to 102, 103...... like this.

    wave is time variable

    Please check the attached file for the data shape.
    Click image for larger version

Name:	like this.PNG
Views:	1
Size:	20.2 KB
ID:	1592039




    Here`s my question.

    If var1 has 2 or 3, I'd like to keep all the IDs.

    If var1 doesn't have 2 or 3, I want to get rid of all of that ID.

    So I wrote down like

    Code:
    gen nid=ID if var1 == 2 | var1 == 3
    gen nnid =1 if nid < .
    I don't know what kind of logic to proceed from now on.

    Using loop(ex.forvalues) and "keep if" don't seem to work properly.

    Code:
    gen nid=ID if var1 == 2 | var1 == 3
    gen nnid =1 if nid < .
    keep if nnid == 1
    OR

    Code:
    gen nid=ID if var1 == 2 | var1 == 3
    gen nnid =1 if nid < .
    forvalues wave = 1/22{
        keep if nnid == 1
        }
    It`s not work and I know why this code doesn`t bring me to answer

    But I don`t know how do i solve this case.

    Please give me an advice on what logic to proceed with.

    I hope this article followed the Advice on posting to Statalist.

    Thankyou

  • #2
    Could you confirm if the following delivers what you're after?

    Code:
    gen double yes23 = [var1 == 2 | var1 == 3]
    sort ID, stable
    by ID: egen double min_yes23 = min(yes23)
    drop if min_yes 23 == 0
    drop yes23 min_yes23

    Comment


    • #3
      Hong Il Yoo gave a helpful answer, but the code can be slimmed down:

      Code:
      bysort ID: egen double min_yes23 = min(inlist(var1, 2, 3))
      drop if min_yes 23 == 0
      drop min_yes23
      The general problem I call the creation of "any" variables -- any observations in a group match a criterion? -- and "all" variables can be discussed alongside.

      FAQ https://www.stata.com/support/faqs/d...ble-recording/

      Before Googling wildly, scanning the Stata FAQs is a good idea. Usually, somebody else had the same problem earlier!
      Last edited by Nick Cox; 30 Jan 2021, 05:36.

      Comment


      • #4
        Nick Cox Thank you for your advice specially FAQ url. I'll make good use of it.

        Comment


        • #5
          Originally posted by Hong Il Yoo View Post
          Could you confirm if the following delivers what you're after?

          Code:
          gen double yes23 = [var1 == 2 | var1 == 3]
          sort ID, stable
          by ID: egen double min_yes23 = min(yes23)
          drop if min_yes 23 == 0
          drop yes23 min_yes23
          Hong Il Yoo Thank you for your answer.

          But It didn't work as I wished.

          When I used your code some ID was deleted even it has 2or3

          and some ID`s wave was deleted when I used your answer

          the remaining ID is very different from when I used "drop if"

          That is my big problem

          I'll keep looking for solutions. Please let me know if you have a good idea.

          It will be of great help to me. Have a good day

          Comment


          • #6
            I believe what you want is:

            Code:
            bysort ID: egen double max_yes23 = max(inlist(var1, 2, 3))
            drop if max_yes23 == 0
            drop max_yes23
            Which is adapted from @Nick Cox's code in #3, but with max(inlist(var1,2,3)) instead of min(inlist(var1,2,3)), in order to set max_yes23 to 1 if there is at least 1 occurrence of 2 or 3.

            Comment


            • #7
              Originally posted by Kim JongBin View Post

              Hong Il Yoo Thank you for your answer.

              But It didn't work as I wished.

              When I used your code some ID was deleted even it has 2or3

              and some ID`s wave was deleted when I used your answer

              the remaining ID is very different from when I used "drop if"

              That is my big problem

              I'll keep looking for solutions. Please let me know if you have a good idea.

              It will be of great help to me. Have a good day
              As far as I can tell, the only issue with my code example is that I typed -drop if min_yes 23 == 0- when I should have said -drop if min_yes23 == 0-. At least for now, I don't see how it is possible that the code drops some waves and replaces the content of your ID variable. If you can share with us a subset of your data that can be used to replicate the problems that you have mentioned, one of us may be able share a more useful suggestion.

              Comment


              • #8
                Ah yes Ali Atia's observation is spot on, it should have been -max- rather than -min-.

                Comment


                • #9
                  I am corrected too: in essence "any" is returned by taking the maximum over an (0, 1) indicator and "all" by taking the minimum.

                  Comment


                  • #10
                    Originally posted by Ali Atia View Post
                    I believe what you want is:

                    Code:
                    bysort ID: egen double max_yes23 = max(inlist(var1, 2, 3))
                    drop if max_yes23 == 0
                    drop max_yes23
                    Which is adapted from @Nick Cox's code in #3, but with max(inlist(var1,2,3)) instead of min(inlist(var1,2,3)), in order to set max_yes23 to 1 if there is at least 1 occurrence of 2 or 3.
                    Ali Atia

                    Your code is working now!

                    When I used Ali's code, all waves of an ID with any observation 2 or 3 were marked as 1 in max_yes23.

                    The previous code, min_yes23, had only a partial wave of IDs with 2 or 3.

                    And Nick Cox and Hong Il Yoo The advice you gave me was a great help in my study of Stata

                    Your advice will be useful next time.

                    All the advice you gave me became a big study. Thanks. I really appreciate it.

                    I wish you all the best.

                    Comment

                    Working...
                    X