Announcement

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

  • Dropping panel data

    Hi, I am still new to stata and I have encountered problems.

    I have this set of data,

    I only want to keep companies with consecutives year from 2007 to 2016 and 2007 to 2017, what command should I use?

    Thank you
    Company Years
    A 2007
    A 2008
    A 2009
    A 2010
    A 2011
    A 2012
    A 2013
    A 2014
    A 2015
    A 2016
    A 2017
    B 2008
    B 2009
    B 2010
    B 2011
    B 2012
    B 2013
    B 2014
    B 2015

  • #2
    Welcome to Statalist. Please take the time to review the FAQs, especially the part that relates to presenting data examples using dataex. Normally, you won't want to drop observations but rather create an indicator variable which you can use to select the desired set of observations. Nonetheless, here is a solution to your question.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str1 company int years
    "A" 2007
    "A" 2008
    "A" 2009
    "A" 2010
    "A" 2011
    "A" 2012
    "A" 2013
    "A" 2014
    "A" 2015
    "A" 2016
    "A" 2017
    "B" 2008
    "B" 2009
    "B" 2010
    "B" 2011
    "B" 2012
    "B" 2013
    "B" 2014
    "B" 2015
    end
    bys company: egen tag= total(inrange(year, 2007, 2017))
    bys company: keep if tag>10

    Comment


    • #3
      Something like this should do the trick:

      Code:
      . egen numyears = total(1) if year>=2007 & year<=2017, by(company)
      
      
      . keep if numyears>=10
      (8 observations deleted)

      Comment


      • #4
        Originally posted by Andrew Musau View Post
        Welcome to Statalist. Please take the time to review the FAQs, especially the part that relates to presenting data examples using dataex. Normally, you won't want to drop observations but rather create an indicator variable which you can use to select the desired set of observations. Nonetheless, here is a solution to your question.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str1 company int years
        "A" 2007
        "A" 2008
        "A" 2009
        "A" 2010
        "A" 2011
        "A" 2012
        "A" 2013
        "A" 2014
        "A" 2015
        "A" 2016
        "A" 2017
        "B" 2008
        "B" 2009
        "B" 2010
        "B" 2011
        "B" 2012
        "B" 2013
        "B" 2014
        "B" 2015
        end
        bys company: egen tag= total(inrange(year, 2007, 2017))
        bys company: keep if tag>10
        Hi, I apologiz for posting in wrong format. I have read and install dataex. Thank you for your help.

        Comment

        Working...
        X