Announcement

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

  • How to identify city names appeared in all years

    I am analyzing a panel data. But I don't know how to get the list of city names which have values in all years.

    For example, the cities appear in all year in the following data are A, B, C. Does anyone know how to do this?

    Code:
     
    Year City
    1999 A
    1999 B
    1999 C
    1999 D
    1999 E
    1999 F
    1999 G
    2000 A
    2000 B
    2000 C
    2000 D
    2000 E
    2001 A
    2001 B
    2001 C
    ​​​​​​​

  • #2
    Victor:
    you may want to try:
    Code:
    bysort City (Year): gen flag=1 if Year[_N]-Year[_n]==2
    list City if flag==1
    
         +------+
         | City |
         |------|
      1. |    A |
      4. |    B |
      7. |    C |
         +------+
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      In this example, a city appearing in all years is equivalent to the frequency of observations being 3. Therefore

      Code:
      clear 
      input Year str1 City
      1999    A
      1999    B
      1999    C
      1999    D
      1999    E
      1999    F
      1999    G
      2000    A
      2000    B
      2000    C
      2000    D
      2000    E
      2001    A
      2001    B
      2001    C
      end 
      
      bysort City (Year) : gen allyears = _N == 3 
      
      list if allyears, sep(3)  
      
           +------------------------+
           | Year   City   allyears |
           |------------------------|
        1. | 1999      A          1 |
        2. | 2000      A          1 |
        3. | 2001      A          1 |
           |------------------------|
        4. | 1999      B          1 |
        5. | 2000      B          1 |
        6. | 2001      B          1 |
           |------------------------|
        7. | 1999      C          1 |
        8. | 2000      C          1 |
        9. | 2001      C          1 |
           +------------------------+
      
      list City if allyears & Year == 1999 
      
           +------+
           | City |
           |------|
        1. |    A |
        4. |    B |
        7. |    C |
           +------+

      Comment


      • #4
        Thanks much Carlo and Nick! Problem solved, the _N command is super useful

        Comment

        Working...
        X