Announcement

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

  • Creating a panel based on two date

    Hi Statalist,

    I am fairly new to Stata and this forum. I appreciate any help.

    My current data has three columns. The first column functions as ID and the second and third columns are two date in %tm format (Please see attached screenshot).

    I would like to generate a new variable MonthYear and make this data into a panel. MonthYear is the month between date in column 2 and column 3. For example, the final data should have 60 rows for A01. The MonthYear variable will be 1997m4 for the first row, 1997m5 for the second row, ..., and 2002m4 for the last row.


    Click image for larger version

Name:	Screen Shot 2022-08-01 at 2.36.14 PM.png
Views:	1
Size:	87.7 KB
ID:	1676063

    Thank you very much.


    Last edited by Yifei Liu; 01 Aug 2022, 13:45.

  • #2
    This is not tested due to lack of a usable data example. See FAQ Advice #12 for details on how to present data examples using the dataex command.

    Code:
    g toexpand= InserviceDate- QueueDate +1
    expand toexpand
    bys QueueNumber: g myear= QueueDate+_n-1
    format myear %tm
    encode QueueNumber, g(id)
    xtset id myear

    Comment


    • #3
      Thanks Andrew Musau! This works very well. And I will for sure follow #12 when I ask questions next time.

      Comment

      Working...
      X