Announcement

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

  • Keep if another variable equals?

    Hi all,

    Apologies, but I have an admittedly rudimentary question on how to filter my data set. As a minimal working example, say I have the following,

    Code:
    ssc install dataex
    clear
    input byte id int year int variable
    1   1980    1
    1   1981    5
    1   1982    2
    2   1980    0
    2   1981    2
    2   1982    4
    3   1980    1
    3   1981    0
    3   1982    0
    4   1980    5
    4   1981    1
    4   1982    4     
    end
    list
    I'd like to keep IDs that exhibit a certain value on another variable at any point in time. Using the above minimal example, I'd like to keep all ID's that scored a 5 on my variable and yield the following,

    Code:
         +----------------------+
         | id   year   variable |
         |----------------------|
      1. |  1   1980          1 |
      2. |  1   1981          5 |
      3. |  1   1982          2 |
      4. |  4   1980          5 |
      5. |  4   1981          1 |
         |----------------------|
      6. |  4   1982          4 |
         +----------------------+
    I attempted to use keep id if variable == 5 but invalid. I suspect I'm missing something rather obvious and appreciate suggestions. Thank you in advance!

  • #2
    Why don't you simply type:

    Code:
    bysort id: egen a = max(variable)
    keep if a == 5
    drop a
    Last edited by Pantelis Kazakis; 08 Jan 2021, 10:56.

    Comment


    • #3
      Originally posted by Pantelis Kazakis View Post
      Why don't you simply type:
      Code:
      keep if variable == 5
      ?
      Thank you for the prompt response! Unfortunately, that code yields the following,

      Code:
           +----------------------+
           | id   year   variable |
           |----------------------|
        1. |  1   1981          5 |
        2. |  4   1980          5 |
           +----------------------+
      Whereas I'm trying to keep all instances of a ID so long as it exhibits a certain value (i.e. 5) on another variable, such as the following,

      Code:
           +----------------------+
           | id   year   variable |
           |----------------------|
        1. |  1   1980          1 |
        2. |  1   1981          5 |
        3. |  1   1982          2 |
        4. |  4   1980          5 |
        5. |  4   1981          1 |
           |----------------------|
        6. |  4   1982          4 |
           +----------------------+

      Comment


      • #4
        ^ See the modified code. I had to read again to see what you meant.

        Comment


        • #5
          Originally posted by Pantelis Kazakis View Post
          ^ See the modified code. I had to read again to see what you meant.
          Perfect! In retrospect I should have figured that out. Thank you for quickly helping to solve my question!

          Comment


          • #6
            Originally posted by Jeff Tree View Post

            Perfect! In retrospect I should have figured that out. Thank you for quickly helping to solve my question!
            Yes, this just for the case of the max value of the variable. However, other code might be needed if you want other values, not necessarily maximum or minimum etc.

            Comment

            Working...
            X