Announcement

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

  • How to index observations by date in panel data with duplicate dates?

    Hello,

    I have a dataset that's set up as panel data with multiple responses for each participant. I have a date variable that has many duplicates within each participant. I want to create a variable that indexes the observations by date for each participant, but I want each duplicate date to have the same _n. So for example, if the first two dates are the same, they'd both be _n = 1 and then the next date would be _n = 2 and so on. Does anyone know how to do this? I can't find an answer anywhere.

  • #2
    So let's say your participant identification variable is called id, and the date variable is called date. I also assume that date is an actual Stata internal format numeric variable, not a string or some long integer that human eyes might read as a date.

    Code:
    by id date, sort: gen date_sequence = 1 if _n == 1
    by id: replace date_sequence = sum(date_sequence)
    In the future, when asking for help with code, please show example data, and use the -dataex- command to do that. 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.

    Whenever you ask for help with code, show example data. Whenever you show example data, use -dataex-.

    Comment


    • #3
      Thank you, Clyde! I tried this and it worked. I am fairly new to Stata and the forum, so I appreciate the tip as well!

      Comment

      Working...
      X