Announcement

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

  • Add dummy variable to ids within a certain time period.

    Hello,

    I am working on a panel data where the panel variable is called ID (unbalanced) and time variable is months with gaps.

    I want to make a variable that groups my time variable months and ID for a two year period. However, as I have gaps in my dataset, I cannot figure out how to group it based on dates.

    As you see in the example below there is a gap between 1980m7 and 1980m9, and thus this two year period only has 23 months. However, I still want the group to change at the beginning of 1982. Anyone know a solution to this?

    Months ID Return Dummy variable(As I want it)
    1980m1 1 -0.01 1
    1980m2 1 0.014 1
    1980m3 1 0.02 1
    1980m4 1 0.003 1
    1980m5 1 -0.0012 1
    1980m6 1 -0.002 1
    1980m7 1 -0.01 1
    1980m9 1 0.12 1
    1980m10 1 0.019 1
    1980m11 1 0.017 1
    1980m12 1 0.005 1
    1981m1 1 0.0032 1
    1981m2 1 0.00684 1
    1981m3 1 0.00239 1
    1981m4 1 0.00005 1
    1981m5 1 -0.009 1
    1981m6 1 -0.0007 1
    1981m7 1 0.005 1
    1981m8 1 -0.0003 1
    1981m9 1 0.0078 1
    1981m10 1 0.0039 1
    1981m11 1 -0.0083 1
    1981m12 1 -0.0099 1
    1982m1 1 -0.01 2

  • #2
    Here is code that seems to do what you want on your sample data.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(Months ID Return)
    240 1   -.01
    241 1   .014
    242 1    .02
    243 1   .003
    244 1 -.0012
    245 1  -.002
    246 1   -.01
    248 1    .12
    249 1   .019
    250 1   .017
    251 1   .005
    252 1  .0032
    253 1 .00684
    254 1 .00239
    255 1 .00005
    256 1  -.009
    257 1 -.0007
    258 1   .005
    259 1 -.0003
    260 1  .0078
    261 1  .0039
    262 1 -.0083
    263 1 -.0099
    264 1   -.01
    end
    format %tm Months
    generate year = yofd(dofm(Months))
    generate Dummy = floor((year-1980)/2)+1
    list, sepby(year)
    Code:
    . list, sepby(year) noobs
    
      +--------------------------------------+
      |  Months   ID   Return   year   Dummy |
      |--------------------------------------|
      |  1980m1    1     -.01   1980       1 |
      |  1980m2    1     .014   1980       1 |
      |  1980m3    1      .02   1980       1 |
      |  1980m4    1     .003   1980       1 |
      |  1980m5    1   -.0012   1980       1 |
      |  1980m6    1    -.002   1980       1 |
      |  1980m7    1     -.01   1980       1 |
      |  1980m9    1      .12   1980       1 |
      | 1980m10    1     .019   1980       1 |
      | 1980m11    1     .017   1980       1 |
      | 1980m12    1     .005   1980       1 |
      |--------------------------------------|
      |  1981m1    1    .0032   1981       1 |
      |  1981m2    1   .00684   1981       1 |
      |  1981m3    1   .00239   1981       1 |
      |  1981m4    1   .00005   1981       1 |
      |  1981m5    1    -.009   1981       1 |
      |  1981m6    1   -.0007   1981       1 |
      |  1981m7    1     .005   1981       1 |
      |  1981m8    1   -.0003   1981       1 |
      |  1981m9    1    .0078   1981       1 |
      | 1981m10    1    .0039   1981       1 |
      | 1981m11    1   -.0083   1981       1 |
      | 1981m12    1   -.0099   1981       1 |
      |--------------------------------------|
      |  1982m1    1     -.01   1982       2 |
      +--------------------------------------+
    Note the use of the dataex command to present sample data in a usable format. Please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. See especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

    Comment


    • #3
      Thank you, this worked perfectly. And yes, will review the FAQ again.

      Comment

      Working...
      X