Announcement

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

  • Panel data question

    I really need help! I am working with panel data for 8 years and am trying to get Stata to count the total number of no's indicated after the first yes indication. My data is in long form, so it looks like this:

    1999 No
    2001 No
    2003 Yes
    2005 No
    2007 Yes
    2009 No
    2011 No
    2013 No

    In the example above, there are 4 no's after the first yes, but I can only get Stata to recognize the first no after the first yes.


  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year str3 yesno float panel
    1999 "No"  1
    2001 "No"  1
    2003 "Yes" 1
    2005 "No"  1
    2007 "Yes" 1
    2009 "No"  1
    2011 "No"  1
    2013 "No"  1
    end
    
    by panel (year), sort: egen year_first_yes = min(cond(yesno == "Yes", year, .))
    by panel: egen wanted = total(cond(year > year_first_yes, yesno == "No", .))
    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.


    Also, please make your example data reflect the problem. You speak of having panel data, but you post only a single time series, and not even a panel variable. The code for dealing with a single time series would be different (and slightly simpler), but would not work for multiple panels.

    Comment


    • #3
      Thank you for your help. I apologize for using the wrong language to describe the data as well as not knowing what I do not know. Luckily, there are people who are more familiar with STATA, like you, who can guide simpletons like me. Your code worked and now I can move on with my longitudinal analysis- for that I am grateful.

      Comment

      Working...
      X