Announcement

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

  • Panel data - dropping all observations after a switch

    Hi everyone,

    I have panel data below (just one id shown), it is sorted by id and date
    I want to be able to drop all observations once the type is different from above - i.e. I only want to keep the observations below nn<=45
    How can I code this so that the order of 1 or 2 doesn't matter (some id have first type as 2, some have first type as 1 - I want to keep only the observations where the first type occurs but none after they switch, even if they switch back)

    any help would be appreciated

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long id int date byte type float nn
    165618 16819 1   1
    165618 16845 1   2
    165618 16867 1   3
    165618 16887 1   4
    165618 16897 1   5
    165618 16918 1   6
    165618 16940 1   7
    165618 16965 1   8
    165618 16987 1   9
    165618 17010 1  10
    165618 17034 1  11
    165618 17058 1  12
    165618 17080 1  13
    165618 17106 1  14
    165618 17129 1  15
    165618 17153 1  16
    165618 17177 1  17
    165618 17199 1  18
    165618 17221 1  19
    165618 17245 1  20
    165618 17274 1  21
    165618 17303 1  22
    165618 17331 1  23
    165618 17359 1  24
    165618 17386 1  25
    165618 17421 1  26
    165618 17456 1  27
    165618 17485 1  28
    165618 17513 1  29
    165618 17539 1  30
    165618 17567 1  31
    165618 17594 1  32
    165618 17619 1  33
    165618 17645 1  34
    165618 17671 1  35
    165618 17696 1  36
    165618 17723 1  37
    165618 17748 1  38
    165618 17772 1  39
    165618 17797 1  40
    165618 17806 1  41
    165618 17811 1  42
    165618 17834 1  43
    165618 17868 1  44
    165618 17902 1  45
    165618 17918 2  46
    165618 17930 2  47
    165618 17930 1  48
    165618 17958 2  49
    165618 17958 1  50
    165618 17986 1  51
    165618 18014 2  52
    165618 18014 1  53
    165618 18028 2  54
    165618 18031 2  55
    165618 18039 2  56
    165618 18056 2  57
    165618 18057 1  58
    165618 18070 2  59
    165618 18084 2  60
    165618 18088 1  61
    165618 18088 2  62
    165618 18088 1  63
    165618 18100 2  64
    165618 18100 1  65
    165618 18129 2  66
    165618 18129 1  67
    165618 18161 1  68
    165618 18161 2  69
    165618 18189 1  70
    165618 18189 2  71
    165618 18213 1  72
    165618 18213 2  73
    165618 18241 2  74
    165618 18241 1  75
    165618 18274 2  76
    165618 18274 1  77
    165618 18274 1  78
    165618 18274 2  79
    165618 18302 1  80
    165618 18302 2  81
    165618 18329 1  82
    165618 18329 2  83
    165618 18351 1  84
    165618 18351 2  85
    165618 18381 2  86
    165618 18381 1  87
    165618 18415 1  88
    165618 18415 2  89
    165618 18441 2  90
    165618 18441 1  91
    165618 18465 2  92
    165618 18465 1  93
    165618 18492 1  94
    165618 18519 1  95
    165618 18553 1  96
    165618 18561 1  97
    165618 18577 1  98
    165618 18605 1  99
    165618 18631 1 100
    end
    format %tdnn/dd/CCYY date

  • #2
    I think I found a way:
    Code:
    bysort id (date): gen switch_=1 if type!=type[_n-1]
    bysort id (date): gen switch_sum= sum(switch_)
    drop if switch_sum>1

    Comment


    • #3
      Thanks for posting a nice answer.

      Now that you've seen that, note that it condenses to

      Code:
      bysort id (date): drop if sum(type!=type[_n-1]) > 1
      Related discussion at https://www.stata.com/support/faqs/d...t-occurrences/

      Comment

      Working...
      X