Announcement

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

  • Keeping variables from a list

    Hello everyone,

    I'd like to know if there's a way to keep in my dataset, the observations that are returned from a list. To give you some context, I have a panel data set, where I want to keep just some IDs that satisfy certain condition on the initial year. So I get the list of the IDs that I need, but it's too large to write a keep if, so I would like to keep the observations that only have those IDs.

    Below it's the code that returns the IDs (which are called mrun in this database) that I need to keep.

    Code:
    list mrun if cod_ense2==2 & agno==2016 & (cod_grado==1 | cod_grado==2 | cod_grado==3 | cod_grado==4)
    Thanks!

  • #2
    Code:
     cod_ense2==2 & agno==2016 & (cod_grado==1 | cod_grado==2 | cod_grado==3 | cod_grado==4)
    can be abbreviated to

    Code:
    cod_ense2==2 & agno==2016 & inlist(cod_grado, 1, 2, 3, 4)
    and almost certainly to


    Code:
    cod_ense2==2 & agno==2016 & inrange(cod_grado, 1, 4)
    So, your code might be, if I understand correctly,

    Code:
    egen wanted = max(cod_ense2==2 & agno==2016 & inrange(cod_grado, 1, 4)), by(mrun)
    
    keep if wanted
    Last edited by Nick Cox; 08 Nov 2022, 16:29.

    Comment


    • #3
      Thanks so much! It worked!

      Comment

      Working...
      X