Announcement

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

  • keep variable with condition and time series

    Hi all:

    I want to keep variable use condition with next year and last year data.

    Sample

    ID YEAR CONDITION
    A 1990 .
    B 1990 .
    B 1991 .
    B 1992 1
    B 1993 .
    C 1990 .
    .


    when I use" keep if", the result is B 1992 1, but I want result include both last year and next year data like:
    " B 1991.
    B 1992 1
    B 1993. "

    Many thanks



  • #2
    Anyone could help me? So many thanks!

    Comment


    • #3
      Please use dataex for your data examples (FAQ Advice #12).

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str1 id int year byte condition
      "A" 1990 .
      "B" 1990 .
      "B" 1991 .
      "B" 1992 1
      "B" 1993 .
      "C" 1990 .
      end
      
      bysort id (year) : keep if condition < . | condition[_n-1] < . | condition[_n+1] < .
      
      list
      
           +----------------------+
           | id   year   condit~n |
           |----------------------|
        1. |  B   1991          . |
        2. |  B   1992          1 |
        3. |  B   1993          . |
           +----------------------+

      Comment


      • #4
        Originally posted by Nick Cox View Post
        Please use dataex for your data examples (FAQ Advice #12).

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str1 id int year byte condition
        "A" 1990 .
        "B" 1990 .
        "B" 1991 .
        "B" 1992 1
        "B" 1993 .
        "C" 1990 .
        end
        
        bysort id (year) : keep if condition < . | condition[_n-1] < . | condition[_n+1] < .
        
        list
        
        +----------------------+
        | id year condit~n |
        |----------------------|
        1. | B 1991 . |
        2. | B 1992 1 |
        3. | B 1993 . |
        +----------------------+
        Thank you so much! And I will use dataex next time.

        Comment

        Working...
        X