Announcement

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

  • year and week (YYYYwW)

    Hi

    I will like to format a date (like 01may2020) to at year and a week (2020w16)

    I have tried:

    generate week = week(date)

    format week %tw


    The result gives the correct week but counts from year 1960...




    Furthermore I have tried:

    generate week = weekly(date, "YW")
    But Stata says type mismatch
    cs

  • #2
    Weekly dates in Stata are weeks such that week 0 is, or if you prefer was, the first week in 1960. (I was there, or is it then. Perhaps mostly reading books and playing with whatever Christmas presents I had got.)

    week() is a function to extract the week of the year from a numeric daily date. So 1 April 2021 is in Stata terms in week 13 of 2021.

    weekly() is a function to extract weekly dates from a string variable.

    This is all explained in more technical form in the documentation.

    But what you want is nothing to with functions. It is, as you say, a matter of assigning display formats. Rummaging around in the help for datetime display formats I fumbled my way towards

    Code:
    . di %tdCY!ww mdy(4,1,2021)
    2021w13
    All that said, watch out. Stata's weeks are ... Stata's weeks, with the following rules:

    1. Week 1 of a given year starts on 1 January, always. And week 2 on 8 January. And so on.

    2. Week 52 is the last week of the year, always 8 or 9 days long, depending on whether the year is not or is leap.

    These rules have one advantage, apart from being explicit: Weeks nest within years.

    If these rules are your rules, you're good.

    I've never heard that anyone or any organisation beyond Stata uses these rules. Beyond that, the problem with standard definitions is that there are so many standards to choose from. There are many rules that weeks are deemed to start on one designated day of the week, or equivalently end on another, but such rules still leave open how weeks are described and in particular what happens about partial weeks whenever they fall at the beginning or the end of the year.

    Much more could be said. Any other definition that is worth attention will be programmable.

    Comment


    • #3
      Hi Nick

      thanks a lot for your response

      My days are written a bit different from your, bc my days is generated like: DMY instead of MDY, which gives me a problem with your code.

      When I write:

      di %tdCY!ww dmy(timevariable)

      stata says: unknow function "DMY"


      And just to be sure that I have understand you correct: I can write my datevariable in the parentes instead of write every single day into the code, right?

      cs

      Comment


      • #4
        You are confusing how your dates are displayed - the result of the format %tdCY!ww - with how they are stored - as a Stata daily date representing the number of days since Jan 1 1960.

        Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

        All Stata manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.


        Comment


        • #5
          On the contrary: I am in Britain and prefer to write daily dates like 28 March 2021 or (as in #2) 1 April 2021. At the same time, it's not difficult to understand 9/11 as having its present meaning or (happier example) to know that in the US that March 14 is pi day and June 28 is tau day.

          What bites us both, mildly, is that Stata is from the United States and so sometimes US conventions are taken as standard. There is no Stata function dmy(), as you have found. mdy() is what is available for that specific purpose and it expects month day and year in exactly that order.

          But you can write the same example any other way you like that is legal. Here's one

          Code:
          . di %tdCY!ww daily("1 April 2021", "DMY")
          2021w13
          #1 showed that you know how to use the format command, so the implication -- if and only if you are happy with Stata's definition of weeks -- is that

          format date %tdCY!ww

          is what you are seeking. But this just controls what you see. It doesn't convert your daily dates to weekly dates. All that changing a display format changes is what is displayed. It does not change what is stored.

          So, we are in the 13th week of 2021, in Stata's sense. If we ask for a weekly display format, the same week is shown for the 7 different days in that week.


          Code:
          . clear
          
          . set obs 7
          number of observations (_N) was 0, now 7
          
          . gen test = mdy(4,1,2021) + _n - 7
          
          . list, sep(7)
          
               +-------+
               |  test |
               |-------|
            1. | 22365 |
            2. | 22366 |
            3. | 22367 |
            4. | 22368 |
            5. | 22369 |
            6. | 22370 |
            7. | 22371 |
               +-------+
          
          . format test %td 
          
          . list, sep(7)
          
               +-----------+
               |      test |
               |-----------|
            1. | 26mar2021 |
            2. | 27mar2021 |
            3. | 28mar2021 |
            4. | 29mar2021 |
            5. | 30mar2021 |
            6. | 31mar2021 |
            7. | 01apr2021 |
               +-----------+
          
          . format test %tdCY!ww
          
          . list, sep(7)
          
               +---------+
               |    test |
               |---------|
            1. | 2021w13 |
            2. | 2021w13 |
            3. | 2021w13 |
            4. | 2021w13 |
            5. | 2021w13 |
            6. | 2021w13 |
            7. | 2021w13 |
               +---------+
          
          . format test %5.0f 
          
          . list, sep(7) 
          
               +-------+
               |  test |
               |-------|
            1. | 22365 |
            2. | 22366 |
            3. | 22367 |
            4. | 22368 |
            5. | 22369 |
            6. | 22370 |
            7. | 22371 |
               +-------+

          Comment


          • #6
            Thanks a lot Nick!!! You are a big help!!

            But... I have a new problem now.

            In my dataark, the time variable now shows YYYYwWW, but when I click on a random week, the precise day is shown...

            Last edited by Caroline Christine; 01 Apr 2021, 10:10.
            cs

            Comment


            • #7
              Which is a problem when i estimates my model.. Bc I dont want all days in the time serie to be includes as a dummy but only the week (which is less dummies than days)
              cs

              Comment


              • #8
                You are interested in a year-week variable. Changing the display format does nothing to the underlying variable.

                Code:
                gen wanted=yw(year(date), week(date))
                format wanted %tw

                where "date" is your original date variable. Then you use the wanted variable.

                Comment


                • #9
                  Andrew Musau has already explained what to do, but in general #6 is not a new problem at all: it is precisely what I have tried to explain. Changing the display format is just that and nothing else. The original question was about changing the format, but you want a different kind of date.

                  Comment


                  • #10
                    Thanks, Andrew

                    It is exactly the code I asked for!!!
                    cs

                    Comment

                    Working...
                    X