Announcement

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

  • #16
    Nick,

    I really appreciate your help! Just need some time to understand all this stuff.

    Comment


    • #17
      Nick Cox I know this is an old post, but I want to thank you for taking all the time to answer these questions. I came here trying to figure out why my weeks were starting on Wednesday, and how to get them to start on Monday. The article "Stata tip 68: Week assumptions" gave me the answer to both: 1) Because 1-1-2020 was a Wednesday, and b) "gen week = date - mod(dow(date) - 1, 7)".

      So thanks for all you do to help out here.

      Comment


      • #18
        Originally posted by Nick Cox View Post
        The earlier posts in the thread explain that Stata's weeks and thus Stata's %tw format are not what you want.

        Once again, Stata's weeks follow these rules:

        1. Week 1 in a year always starts on 1 January.

        2. Weeks 2/52 start between 7 and 357 days after 1 January,

        3. Week 52 always is 8 or 9 days long.

        Nothing in those rules implies that weeks start on Sunday. Some weeks will and some won't. Whenever 1 January is a Sunday then all weeks start on Sunday in the same year, including the extra-long week 52 (except for the week that starts just before the end of the year!).

        Here for example is some Mata code. First as a warm-up I put the daily dates 23 to 31 December 2017 in a vector:

        Code:
        . mata
        
        : strofreal(mdy(12, (23..31), 2017), "%td")
        1 2 3 4 5 6 7 8 9
        +-------------------------------------------------------------------------------------------------------------+
        1 | 23dec2017 24dec2017 25dec2017 26dec2017 27dec2017 28dec2017 29dec2017 30dec2017 31dec2017 |
        +-------------------------------------------------------------------------------------------------------------+
        
        : wofd(mdy(12, (23..31), 2017))
        1 2 3 4 5 6 7 8 9
        +----------------------------------------------------------------+
        1 | 3014 3015 3015 3015 3015 3015 3015 3015 3015 |
        +--------------------------------------------------------------+
        As above all the last 8 days are in week 3015.

        In 2016, a leap year, the last 9 days are in the same week, 2963, or 2016w52.

        Code:
        : wofd(mdy(12, (23..31), 2016))
        1 2 3 4 5 6 7 8 9
        +----------------------------------------------------------------+
        1 | 2963 2963 2963 2963 2963 2963 2963 2963 2963 |
        +----------------------------------------------------------------+
        
        : strofreal(wofd(mdy(12, (23..31), 2016)), "%tw")
        1 2 3 4 5 6 7 8 9
        +-------------------------------------------------------------------------------------------+
        1 | 2016w52 2016w52 2016w52 2016w52 2016w52 2016w52 2016w52 2016w52 2016w52 |
        +-------------------------------------------------------------------------------------------+
        So, the references in #5 remain relevant reading for applying your own definition, which matches the dow() function but otherwise has nothing to do with the syntax you're looking at.

        Please post a workable data example (FAQ Advice #12) and I will come back with concrete code for what may help more.

        Hello Nick,

        I exactly experience what you demonstrate in this example. I get the error "repeated time values" error when I tsset.

        I've read the papers you mention in #5, however, I still can not fully figure out the problem.

        Could you please explain the situation by giving more specific example?

        Thanks.

        Comment


        • #19
          omerlast There are detailed specific examples in #10 #11 #13 of my posts here, so I am at a loss to know what else to say.

          In any case, all you report here is an error message when you tsset and the request is the other way round, for you to provide detail of

          1. The precise tsset command you tried.

          2. Your dataset, as given by a dataex example.

          It's also not clear that your question has anything to do with weekly data.

          Comment

          Working...
          X