Announcement

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

  • Generate a week from a data and "xtset" the data

    Dear Stata Users,
    I have the sample of data attached below. I need to create a “week” date that goes from Wednesday till Wednesday. For this, I used the following code:
    Code:
    gen week_start = cond(dow(date) >= 3, date - dow(date) + 3, date - dow(date) - 4)
    The problem is that it generates a date variable (with a difference of 7 between two dates) that I cant format into %tw, so that stata understands the id and time variable:
    Code:
    xtset permno week_start
    Can you, please help me to resolve this issue.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double permno long date
    10000 9502
    10000 9503
    10000 9504
    10000 9505
    10000 9506
    10000 9509
    10000 9510
    10000 9511
    10000 9512
    10000 9513
    10000 9516
    10000 9517
    10000 9518
    10000 9519
    10000 9520
    10000 9523
    10000 9524
    10000 9525
    10000 9526
    10000 9527
    10000 9530
    10000 9531
    10000 9532
    10000 9533
    10000 9534
    10000 9537
    10000 9538
    10000 9539
    10000 9540
    10000 9541
    10000 9545
    10000 9546
    10000 9547
    10000 9548
    10000 9551
    10001 9510
    10001 9511
    10001 9512
    10001 9513
    10001 9516
    10001 9517
    10001 9518
    10001 9519
    10001 9520
    10001 9523
    10001 9524
    10001 9525
    10001 9526
    10001 9527
    10001 9530
    10001 9531
    10001 9532
    10001 9533
    10001 9534
    10001 9537
    10001 9538
    10001 9539
    10001 9540
    10001 9541
    10001 9545
    10001 9546
    10001 9547
    10001 9548
    10001 9551
    10001 9552
    10001 9553
    10001 9554
    10001 9555
    10001 9558
    10001 9559
    10001 9560
    10001 9561
    10001 9562
    10001 9565
    10001 9566
    10001 9567
    10001 9568
    10001 9569
    10001 9572
    10001 9573
    10001 9574
    10001 9575
    10001 9576
    10001 9579
    10001 9580
    10001 9581
    10001 9582
    10001 9586
    10001 9587
    10001 9588
    10001 9589
    10001 9590
    10001 9593
    10001 9594
    10001 9595
    10001 9596
    10001 9597
    10001 9600
    10001 9601
    10001 9602
    end
    format %d date

  • #2
    Stata has week variables, but I don't think they will be useful to you. They cannot be forced to begin on Wednesday, and in fact a new week always starts on January 1.

    So I think you will have to just settle for week_start the way you have defined it.

    That said, you will not be able to -xtset permno week_start- no matter what you do, because you will encounter the "repeated time values within panel" error: you have up to 7 observations per form for any week, because you have daily data.

    Comment


    • #3
      Clyde Schechter pointed out a key problem. Here are some more.

      You shouldn't even try to format this new date as weekly. It's still a daily date. In particular,

      1. Today is a Wednesday and 21642 days after 1 January 1960. If you tell Stata that's a weekly date, then you're referring to the 21642nd week since 1 January 1960. That's a long way into the future.

      2. Stata's weekly formats have no idea that weeks can span years and that there can be 53 weeks starting in a year, which is what your definition implies.

      Weeks running from Wednesday to Wednesday are 8 days long and overlap. I guess you don't mean that.

      Stata understands fine that daily dates can be 7 days apart. Just use the delta() option of tsset and xtset. That solves all your problems -- except the one Clyde pointed out.

      ---------------------------

      PS You are not good at closing threads. Closing means reporting on the outcome, even if the report is that the code didn't work or you're still puzzled.

      I looked at your recent posts and found that you closed none of the last four threads you started

      https://www.statalist.org/forums/for...able-with-zero

      https://www.statalist.org/forums/for...an-correlation

      https://www.statalist.org/forums/for...erval-of-dates

      https://www.statalist.org/forums/for...-certain-dates

      I stopped there. That's a pattern. That is disappointing -- both to the people who answered you; why we should we bother in future with your questions?-- and to people who might read your posts and wonder what the outcome was.



      Comment


      • #4
        Thank you for the response. I foget to mention that I will than drop duplicates by "permno week_start" (was my fault). I used your suggesting to use "delta" with xtset. My final code looks like:

        Code:
        xtset permno week_start, delta(7 )

        Comment

        Working...
        X