Announcement

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

  • Panel data: keep rows if condition is met

    Hello,

    I have posted my data below. Some individuals have 2 records. I would like to keep those records where an individual has a single record only or where Index==2 (i.e., the 2nd record). I manually created the variable keep to show the records I wish to retain. I would be grateful for advice on how to write code for the variable keep. Thank you!

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(ID Index keep)
    1 1 1
    2 1 .
    2 2 1
    3 1 .
    3 2 1
    4 1 1
    5 1 1
    end


  • #2
    Code:
    bys ID: keep if _N==1| Index==2
    The "or" operator is the pipe. _N defines the number of observations per ID group. See

    Code:
    help operators

    Comment


    • #3
      Thank you for replying so quickly! Perfect for my needs.

      Comment


      • #4
        Code:
        bysort ID (Index) : keep if _n == _N
        would work here too.

        Comment

        Working...
        X