Announcement

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

  • Sequential numbering of non-sequential months in a panel dataset

    Greetings,

    I have a sample dataset of firms and dates (and their stock prices, but that isn't vital to my question so am not including the data) below -

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long firmid float date
    771 20018
    771 20019
    771 20023
    771 20024
    771 20025
    771 20026
    771 20027
    771 20030
    771 20032
    771 20037
    771 20038
    771 20039
    771 20040
    771 20041
    771 20044
    771 20045
    771 20046
    771 20048
    771 20051
    771 20052
    771 20053
    771 20055
    771 20058
    771 20060
    771 20061
    771 20066
    771 20067
    771 20068
    771 20069
    771 20072
    771 20073
    771 20074
    771 20075
    771 20080
    771 20081
    771 20083
    771 20086
    771 20087
    771 20094
    771 20097
    771 20100
    771 20103
    771 20104
    771 20109
    771 20110
    771 20111
    771 20117
    771 20118
    771 20122
    771 20128
    771 20132
    771 20137
    771 20139
    771 20143
    771 20144
    771 20145
    771 20146
    771 20147
    771 20149
    771 20150
    771 20151
    771 20152
    771 20156
    771 20158
    771 20159
    771 20160
    771 20163
    771 20167
    771 20177
    771 20178
    771 20185
    771 20186
    771 20188
    771 20193
    771 20195
    771 20198
    771 20199
    771 20200
    771 20201
    771 20205
    771 20214
    771 20215
    771 20216
    771 20223
    771 20227
    771 20228
    771 20233
    771 20234
    771 20236
    771 20237
    771 20241
    771 20242
    771 20247
    771 20249
    771 20256
    771 20262
    771 20263
    771 20264
    771 20268
    771 20269
    end
    format %td date
    I have a question that seems so awfully basic, but I'm unable to do it so far...I need to create a variable that counts the month a date belongs to, sequentially, from the first month a firm appears in the data (this month is not uniform across firms) till the last month, irrespective of year, and whether a calendar month is missing in between because the stock is illiquid and hasn't been traded.

    As an artificial example, if a firm first appears for multiple dates in Sept 2024, then appears for multiple dates in Oct 2024, Nov 2024, Dec 2024, Jan 2025, March 2025.
    I need to generate "month" as 0 (for all dates in Sept 2024), 1, 2, 3, 4, 5, 6.

    I would be grateful for any help to do this.

    Thanking you in advance,
    Sneha

  • #2
    Code:
    bysort firmid (date) : gen wanted = sum(mofd(date) != mofd(date[_n-1]))

    Comment


    • #3
      Thank you so very much, Nick. This worked exactly, as expected. It is an honor to receive a message from you after learning from your Statalist posts all these years.

      Gratefully,
      Sneha

      Comment

      Working...
      X