Announcement

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

  • Gen week variable

    At the moment I have a variable FirstBought with format YYYYMMDD, e.g. 20120616. This variable is type: float & format: %9.0g.
    I need to generate a variable which tells me which week of the year it is.
    If it is possible this variable should start with week 1 = 01012008 and counts until the end of year 2015, which is in that case week416.
    But if that last is not possible it will be no problem, I already have something to fix that.

    Thanks in advance!

  • #2
    Code:
    gen int week_num = wofd(FirstBought) - wofd(td(31dec2007))
    will get you a serial week number that starts at 1 Jan 2008.

    See -help datetime- for general information about converting among the different Stata internal representations for date and time variables.

    Be aware, however, that in Stata a week is not always 7 days. Stata always begins a new week on 1 January of every year. Years are 365 or 366 days, neither number being a multiple of 7. So the 52nd week of each year is extended as needed to accommodate the extra day(s). It is for this reason that weeks are not convenient units of time for most purposes.

    If you need the number of exactly 7-day periods starting with 1 Jan 2008 serialized I would do it as:

    Code:
    gen int week_7_days_num = floor((FirstBought-td(1jan2008))/7) + 1
    Last edited by Clyde Schechter; 14 Jun 2016, 09:04.

    Comment


    • #3
      First, I completely agree with the conclusion Clyde reaches in his final paragraph and code example, and it is what I suspect what Frans really wants (although there will be more than 416 7-day weeks in the 8 year period given).

      However, I think Clyde overlooked one or more problems with the data. Frans writes
      Code:
      I have a variable FirstBought with format YYYYMMDD, e.g. 20120616. This variable is type: float & format: %9.0g.
      Thus the date in FirstBought is a sequence of digits; it is not a "daily" date stored in Stata Internal Format, which is what is assumed by both code examples that Clyde provided.

      At this point, let me add for Frans's benefit the following advice. If you have not already done so, it would benefit you to work your way through the guidance in help datetime, which is without a doubt the most visited documentation on my system, with the second-most-visited being Chapter 24 (Working with dates and times) of the Stata User's Guide PDF available from the PDF Documentation item on Stata's Help menu. Before working with dates and times, any Stata user should read the very detailed Chapter 24 thoroughly. After that, the help documentation will usually be enough to point the way. Some people may be able to remember everything without have to continually refer to the documentation, but I for one am not such a person.

      Now, a second problem seems to me to arise with this storage of the dates in a float value. The output of help precision tells us

      Floats can store up to 16,777,215 exactly.
      That is eight digits, starting with the digit 1. That suggests to me that the dates in FirstBought - 8 digits starting with a 2 - have not been stored accurately. Consider the following example.
      Code:
      . clear
      
      . set obs 5
      number of observations (_N) was 0, now 5
      
      . generate double dated = 20160100+_n
      
      . generate float datef = 20160100+_n
      
      . format dated datef %9.0f
      
      . list
      
           +---------------------+
           |    dated      datef |
           |---------------------|
        1. | 20160101   20160100 |
        2. | 20160102   20160102 |
        3. | 20160103   20160104 |
        4. | 20160104   20160104 |
        5. | 20160105   20160104 |
           +---------------------+
      
      .
      So I think Frans has two tasks ahead. I will guess that the dates were read as a string value and converted to a float value. Instead, the string should be converted to a Stata Internal Format value, following the guidance in help datetime referenced above. The, the week counter can be calculated following Clyde's second example.

      Comment


      • #4
        William is right. I didn't pay close enough attention to the format: %9.0g. I had assumed that he had float variables corresponding to Stata internal dates and that they were formatted as %tdCCYYNNDD, which would cause them to look like 20160614 for today's date. But if he really has just floats with numbers like 20160614 then he has both a precision problem and needs to convert (the corrected values) to Stata internal dates first. Thanks for pointing that out, William.

        Comment


        • #5
          On handling weeks: pertinent discussions include

          Code:
          . search week, sj
          
          Search of official help files, FAQs, Examples, SJs, and STBs
          
          SJ-12-4 dm0065_1  . . . . . Stata tip 111: More on working with weeks, erratum
                  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                  Q4/12   SJ 12(4):765                                     (no commands)
                  lists previously omitted key reference
          
          SJ-12-3 dm0065  . . . . . . . . . .  Stata tip 111: More on working with weeks
                  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                  Q3/12   SJ 12(3):565--569                                (no commands)
                  discusses how to convert data presented in yearly and weekly
                  form to daily dates and how to aggregate such data to months
                  or longer intervals
          
          SJ-10-4 dm0052  . . . . . . . . . . . . . . . . Stata tip 68: Week assumptions
                  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                  Q4/10   SJ 10(4):682--685                                (no commands)
                  tip on Stata's solution for weeks and on how to set up
                  your own alternatives given different definitions of the
                  week
          Running search week, sj within Stata will give you clickable links to the Stata Journal articles.

          Comment


          • #6
            Code:
            gen int week_num = wofd(FirstBought) - wofd(td(31dec2007))
            Clyde, your code is very useful to me. However, is it possible to generate a weekly variable by year? That would be : week 1 to week 52 for 2012 (01-january to 31-december), as well as week 1 to week 52 for 2013 (01-january to 31-december) and so on,

            It's my first post, thank you very much!

            Comment


            • #7
              Yes, it is possible. That is what the week() function does.

              But you will probably regret it. A year is not 52 weeks, because 52*7 is only 364.. Stata resolves this discrepancy by extending the 52nd week of the year to include any remaining days. In a leap year, that means that week 52 has 9 days, and in any other year it has only 8. All other weeks are 7 days. As a result, calculations with variables containing this version of weeks are, at best, maddeningly complicated, and at worst, impossible altogether. Even something as simple as "how many weeks ago did X happen?" can give spurious results. For example 25 December and 31 December are, by any sensible reckoning, a week apart, but by this reckoning the number of elasped weeks is zero. I suppose if all you are going to do is use this variable to label weeks in a table, and if you can live with the bizarreness of how week 52 is handled, there might be some use to this. But if you plan to do even simple calculations with this variable, you are looking for trouble in a place where you are near guaranteed to find it.

              Comment


              • #8
                Originally posted by Clyde Schechter View Post
                Yes, it is possible. That is what the week() function does.

                But you will probably regret it. A year is not 52 weeks, because 52*7 is only 364.. Stata resolves this discrepancy by extending the 52nd week of the year to include any remaining days. In a leap year, that means that week 52 has 9 days, and in any other year it has only 8. All other weeks are 7 days. As a result, calculations with variables containing this version of weeks are, at best, maddeningly complicated, and at worst, impossible altogether. Even something as simple as "how many weeks ago did X happen?" can give spurious results. For example 25 December and 31 December are, by any sensible reckoning, a week apart, but by this reckoning the number of elasped weeks is zero. I suppose if all you are going to do is use this variable to label weeks in a table, and if you can live with the bizarreness of how week 52 is handled, there might be some use to this. But if you plan to do even simple calculations with this variable, you are looking for trouble in a place where you are near guaranteed to find it.
                Thank you very much!

                Comment


                • #9
                  Hello,

                  I have a set of dates from Jan. 1 2017 till Nov.20 2018 (type: float; format: %td):
                  date
                  01jan2017
                  02jan2017
                  20nov2018
                  I need to generate a variable which tells me which week of the year each date refers to (a week starts with Sunday). This variable, ‘week’, should be in %tw format, i.e.
                  date week
                  01jan2017 2017w1
                  02jan2017 2017w2
                  20nov2018 2018w47
                  I read the discussion above, but the codes here only give me a week number in integer format. How can I generate my ‘week’ variable in %tw format?

                  Thank you!

                  Comment


                  • #10
                    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.


                    Comment


                    • #11
                      No data example yet, but here is some play with a sandbox dataset. I am happy to confess that I move back and forth between logic and data and make lots of small mistakes in private (some of the "off by 1" or fencepost flavour). The small tricks from experience are just

                      1. Work with small examples such that you can independently check against the correct answer.

                      2. Put some effort into finding out about Stata's functions.

                      3. Although I have written on this subject, it's often as much fun, in piano or sudoku practice style, just to play with a new version of the problem.

                      Starting with the first 2 weeks of 2019, we can work out the first Sunday of the year with the help of dow() (which returns 0 for Sundays):

                      Code:
                      . clear
                      
                      . set obs 14
                      number of observations (_N) was 0, now 14
                      
                      . gen ddate = mdy(1, _n, 2019)
                      
                      . gen dow = dow(ddate)
                      
                      . gen first = mdy(1, 1, year(ddate)) + 7 - dow(mdy(1, 1, year(ddate)))
                      
                      . replace first = mdy(1, 1, year(ddate)) if dow(mdy(1, 1, year(ddate))) == 0 
                      
                      . format ddate first %td
                      
                      . list, sep(7)
                      
                           +-----------------------------+
                           |     ddate   dow       first |
                           |-----------------------------|
                        1. | 01jan2019     2   06jan2019 |
                        2. | 02jan2019     3   06jan2019 |
                        3. | 03jan2019     4   06jan2019 |
                        4. | 04jan2019     5   06jan2019 |
                        5. | 05jan2019     6   06jan2019 |
                        6. | 06jan2019     0   06jan2019 |
                        7. | 07jan2019     1   06jan2019 |
                           |-----------------------------|
                        8. | 08jan2019     2   06jan2019 |
                        9. | 09jan2019     3   06jan2019 |
                       10. | 10jan2019     4   06jan2019 |
                       11. | 11jan2019     5   06jan2019 |
                       12. | 12jan2019     6   06jan2019 |
                       13. | 13jan2019     0   06jan2019 |
                       14. | 14jan2019     1   06jan2019 |
                           +-----------------------------+
                      Now let's jump 3 weeks to a time roundabout the present: We need to count in Sundays from the first Sunday in the year:
                      .
                      Code:
                      . replace ddate = ddate + 21
                      (14 real changes made)
                      
                      . replace dow = dow(ddate)
                      (0 real changes made)
                      
                      . gen weekno = ceil((ddate + 1 - first)/7)
                      .
                      . list, sepby(weekno)
                      
                           +--------------------------------------+
                           |     ddate   dow       first   weekno |
                           |--------------------------------------|
                        1. | 22jan2019     2   06jan2019        3 |
                        2. | 23jan2019     3   06jan2019        3 |
                        3. | 24jan2019     4   06jan2019        3 |
                        4. | 25jan2019     5   06jan2019        3 |
                        5. | 26jan2019     6   06jan2019        3 |
                           |--------------------------------------|
                        6. | 27jan2019     0   06jan2019        4 |
                        7. | 28jan2019     1   06jan2019        4 |
                        8. | 29jan2019     2   06jan2019        4 |
                        9. | 30jan2019     3   06jan2019        4 |
                       10. | 31jan2019     4   06jan2019        4 |
                       11. | 01feb2019     5   06jan2019        4 |
                       12. | 02feb2019     6   06jan2019        4 |
                           |--------------------------------------|
                       13. | 03feb2019     0   06jan2019        5 |
                       14. | 04feb2019     1   06jan2019        5 |
                           +--------------------------------------+
                      The example above shows consistency: weekno does jump by 1 each Sunday. A nearby calendar (e.g. on your desk, wall or phone) will confirm that the week number is right.

                      Up in the air still is what you do with any days at the beginning of the year before the first Sunday. I am guessing that the most common convention would be to regard them as included in the last week of the previous year.

                      I'll continue to underline that Stata's week format is useless here. You'll never get it to show week 53 for years in which there 53 Sundays. 2017 was an example.

                      Last edited by Nick Cox; 30 Jan 2019, 14:32.

                      Comment


                      • #12
                        Nick, thank you SO much! Your comments are really helpful.

                        As for my data, here you are

                        Code:
                        * Example generated by -dataex-. To install: ssc install dataex
                        clear
                        input float date int(w_1 w_2) str7 w_3
                        20820 1 1 "2017w1"
                        20821 1 1 "2017w1"
                        20822 1 1 "2017w1"
                        20823 1 1 "2017w1"
                        20824 1 1 "2017w1"
                        20825 1 1 "2017w1"
                        20826 1 1 "2017w1"
                        20827 2 2 "2017w2"
                        20828 2 2 "2017w2"
                        20829 2 2 "2017w2"
                        20830 2 2 "2017w2"
                        20831 2 2 "2017w2"
                        20832 2 2 "2017w2"
                        20833 2 2 "2017w2"
                        20834 3 3 "2017w3"
                        20835 3 3 "2017w3"
                        20836 3 3 "2017w3"
                        20837 3 3 "2017w3"
                        20838 3 3 "2017w3"
                        20839 3 3 "2017w3"
                        end
                        format %td date
                        This is what I have tried so far (I did not take account which day a week starts with):
                        gen int w_1 = wofd(date) - wofd(td(31dec2016))
                        gen int w_2 = week(date)

                        I have also tried your code from #10:
                        gen w_3 = strofreal(wofd(date), "%tw")


                        Now, I am trying to run your code from #11 …


                        Thank you again.

                        Comment


                        • #13
                          As said or implied, none of the apparatus of wofd() or week() or the %tw format can ensure that you get weeks starting on Sundays. It so happens that some Stata weeks also start on Sundays, but otherwise that won't work. My using %tw in #10 was part of the demonstration.

                          Your example data are for the first 20 days of 2017, which doesn't provide a full test as 1 January 2017 was a Sunday. Let's first extend the example to include some days from 2016.

                          Code:
                          * Example generated by -dataex-. To install: ssc install dataex
                          clear
                          input float date
                          20813
                          20814
                          20815
                          20816
                          20817
                          20818
                          20819
                          20820
                          20821
                          20822
                          20823
                          20824
                          20825
                          20826
                          20827
                          20828
                          20829
                          20830
                          20831
                          20832
                          20833
                          20834
                          20835
                          20836
                          20837
                          20838
                          20839
                          end
                          format %td date
                          
                          * a year variable will be useful  
                          gen year = year(date) 
                          
                          * this code to get the first Sunday in each year is a small cosmetic variation on the previous version 
                          gen first_Sun = mdy(1, 1, year) 
                          replace first = mdy(1, 1, year) + 7 - dow(mdy(1, 1, year)) if dow(mdy(1, 1, year)) != 0 
                          format first %td
                          
                          * week in year counts from the first Sunday in each year 
                          gen week_in_y = ceil((date + 1 - first)/7)
                          
                          *  week as a numeric variable uses the first Sunday in 1960 as origin, but use any any other origin 
                          * you might need this for subtractions (this week MINUS that week) 
                          gen week = ceil((date + 1 - mdy(1,3, 1960))/7) 
                          
                          * we can simulate a week display format with a string variable 
                          * copying Stata's conventions exactly is likely to be confusing: you have been warned! 
                          gen sweek = string(year) + " wk " + string(week_in_y) 
                          
                          * so what did we get  
                          list, sepby(week_in_y)
                          
                               +-------------------------------------------------------------+
                               |      date   year   first_Sun   week_i~y   week        sweek |
                               |-------------------------------------------------------------|
                            1. | 25dec2016   2016   03jan2016         52   2974   2016 wk 52 |
                            2. | 26dec2016   2016   03jan2016         52   2974   2016 wk 52 |
                            3. | 27dec2016   2016   03jan2016         52   2974   2016 wk 52 |
                            4. | 28dec2016   2016   03jan2016         52   2974   2016 wk 52 |
                            5. | 29dec2016   2016   03jan2016         52   2974   2016 wk 52 |
                            6. | 30dec2016   2016   03jan2016         52   2974   2016 wk 52 |
                            7. | 31dec2016   2016   03jan2016         52   2974   2016 wk 52 |
                               |-------------------------------------------------------------|
                            8. | 01jan2017   2017   01jan2017          1   2975    2017 wk 1 |
                            9. | 02jan2017   2017   01jan2017          1   2975    2017 wk 1 |
                           10. | 03jan2017   2017   01jan2017          1   2975    2017 wk 1 |
                           11. | 04jan2017   2017   01jan2017          1   2975    2017 wk 1 |
                           12. | 05jan2017   2017   01jan2017          1   2975    2017 wk 1 |
                           13. | 06jan2017   2017   01jan2017          1   2975    2017 wk 1 |
                           14. | 07jan2017   2017   01jan2017          1   2975    2017 wk 1 |
                               |-------------------------------------------------------------|
                           15. | 08jan2017   2017   01jan2017          2   2976    2017 wk 2 |
                           16. | 09jan2017   2017   01jan2017          2   2976    2017 wk 2 |
                           17. | 10jan2017   2017   01jan2017          2   2976    2017 wk 2 |
                           18. | 11jan2017   2017   01jan2017          2   2976    2017 wk 2 |
                           19. | 12jan2017   2017   01jan2017          2   2976    2017 wk 2 |
                           20. | 13jan2017   2017   01jan2017          2   2976    2017 wk 2 |
                           21. | 14jan2017   2017   01jan2017          2   2976    2017 wk 2 |
                               |-------------------------------------------------------------|
                           22. | 15jan2017   2017   01jan2017          3   2977    2017 wk 3 |
                           23. | 16jan2017   2017   01jan2017          3   2977    2017 wk 3 |
                           24. | 17jan2017   2017   01jan2017          3   2977    2017 wk 3 |
                           25. | 18jan2017   2017   01jan2017          3   2977    2017 wk 3 |
                           26. | 19jan2017   2017   01jan2017          3   2977    2017 wk 3 |
                           27. | 20jan2017   2017   01jan2017          3   2977    2017 wk 3 |
                               +-------------------------------------------------------------+
                          Now let's bang on that for the more usual case in which 1 January is not a Sunday. You haven't yet said what you want for days before the first Sunday. Here is one rule coded up.

                          Code:
                          * Example generated by -dataex-. To install: ssc install dataex
                          clear
                          input float date
                          20448
                          20449
                          20450
                          20451
                          20452
                          20453
                          20454
                          20455
                          20456
                          20457
                          20458
                          20459
                          20460
                          20461
                          20462
                          20463
                          20464
                          20465
                          20466
                          20467
                          20468
                          20469
                          20470
                          20471
                          20472
                          20473
                          20474
                          end
                          format %td date
                          
                          gen year = year(date) 
                          gen first_Sun = mdy(1, 1, year) 
                          replace first = mdy(1, 1, year) + 7 - dow(mdy(1, 1, year)) if dow(mdy(1, 1, year)) != 0 
                          format first %td
                          gen week_in_y = ceil((date + 1 - first)/7)
                          gen week = ceil((date + 1 - mdy(1,3, 1960))/7) 
                          gen sweek = string(year) + " wk " + string(week_in_y) 
                          
                          list, sepby(week_in_y)
                          
                               +-------------------------------------------------------------+
                               |      date   year   first_Sun   week_i~y   week        sweek |
                               |-------------------------------------------------------------|
                            1. | 26dec2015   2015   04jan2015         51   2921   2015 wk 51 |
                               |-------------------------------------------------------------|
                            2. | 27dec2015   2015   04jan2015         52   2922   2015 wk 52 |
                            3. | 28dec2015   2015   04jan2015         52   2922   2015 wk 52 |
                            4. | 29dec2015   2015   04jan2015         52   2922   2015 wk 52 |
                            5. | 30dec2015   2015   04jan2015         52   2922   2015 wk 52 |
                            6. | 31dec2015   2015   04jan2015         52   2922   2015 wk 52 |
                               |-------------------------------------------------------------|
                            7. | 01jan2016   2016   03jan2016          0   2922    2016 wk 0 |
                            8. | 02jan2016   2016   03jan2016          0   2922    2016 wk 0 |
                               |-------------------------------------------------------------|
                            9. | 03jan2016   2016   03jan2016          1   2923    2016 wk 1 |
                           10. | 04jan2016   2016   03jan2016          1   2923    2016 wk 1 |
                           11. | 05jan2016   2016   03jan2016          1   2923    2016 wk 1 |
                           12. | 06jan2016   2016   03jan2016          1   2923    2016 wk 1 |
                           13. | 07jan2016   2016   03jan2016          1   2923    2016 wk 1 |
                           14. | 08jan2016   2016   03jan2016          1   2923    2016 wk 1 |
                           15. | 09jan2016   2016   03jan2016          1   2923    2016 wk 1 |
                               |-------------------------------------------------------------|
                           16. | 10jan2016   2016   03jan2016          2   2924    2016 wk 2 |
                           17. | 11jan2016   2016   03jan2016          2   2924    2016 wk 2 |
                           18. | 12jan2016   2016   03jan2016          2   2924    2016 wk 2 |
                           19. | 13jan2016   2016   03jan2016          2   2924    2016 wk 2 |
                           20. | 14jan2016   2016   03jan2016          2   2924    2016 wk 2 |
                           21. | 15jan2016   2016   03jan2016          2   2924    2016 wk 2 |
                           22. | 16jan2016   2016   03jan2016          2   2924    2016 wk 2 |
                               |-------------------------------------------------------------|
                           23. | 17jan2016   2016   03jan2016          3   2925    2016 wk 3 |
                           24. | 18jan2016   2016   03jan2016          3   2925    2016 wk 3 |
                           25. | 19jan2016   2016   03jan2016          3   2925    2016 wk 3 |
                           26. | 20jan2016   2016   03jan2016          3   2925    2016 wk 3 |
                           27. | 21jan2016   2016   03jan2016          3   2925    2016 wk 3 |
                               +-------------------------------------------------------------+
                           
                          gen tofix = week_in_y == 0 
                          gen first_Sun_prev = mdy(1, 1, year - 1) 
                          replace first_Sun_prev = mdy(1, 1, year - 1) + 7 - dow(mdy(1, 1, year - 1)) if dow(mdy(1,  1, year - 1)) != 0 
                          format first_Sun_prev %td 
                          replace week_in_y = ceil((mdy(12, 31, year - 1) + 1 - first_Sun_prev)/7) if tofix
                          replace sweek = string(year - 1) + " wk " + string(week_in_y) if tofix 
                          . 
                          list if tofix 
                          
                               +---------------------------------------------------------------------------------+
                               |      date   year   first_Sun   week_i~y   week        sweek   tofix   first_S~v |
                               |---------------------------------------------------------------------------------|
                            7. | 01jan2016   2016   03jan2016         52   2922   2015 wk 52       1   04jan2015 |
                            8. | 02jan2016   2016   03jan2016         52   2922   2015 wk 52       1   04jan2015 |
                               +---------------------------------------------------------------------------------+
                          So, the first two ways in 2016, previously assigned to 2016 wk 0, are now regarded as the tail end of 2015 wk 52.

                          I have tried to write the code without assuming that all days within a period are present in the dataset.

                          There may be easier ways to do it....

                          Comment


                          • #14


                            Nick Cox

                            Given that (1) Stata's week format is useless and (2) this seems to come up a lot, what would it take to entice you to take what you wrote in post #11 and #13 and convert it into a user-contributed command (maybe called "useful_week")?

                            I could imagine an option to begin the new year on the 1st Sunday (or other day of choice) or the 1st week that ends with a Saturday (as long as it falls at least four or more days into the month.)

                            The latter approach is the one epiweek (SSC) takes.

                            I would do it but I'm not nearly proficient enough yet

                            Comment


                            • #15
                              David Benson What inducement are you offering?

                              Thanks for the encouragement, but sorry: I don't expect to write such a command. I'd rather explain how people can match their own specific needs.

                              I won't align myself with a blanket statement that Stata's weeks are useless. I said that they are useless for Helen's purpose and I've not heard anyone say that they find them useful, but absence of evidence remembered by me is not evidence of absence.

                              Weeks are easy. A week is 7 days long.

                              Weeks are a nightmare. I've seen people using weeks starting on Saturday, Sunday, Monday. I've seen people using weeks ending on Friday. I've seen people using weeks based on non-calendar years. At my own institution it's now week 28, and with good reason. I used one convention above about weeks that span calendar years, but there are others.

                              Weeks are often most simply and best characterised by their key daily dates, whether a start or an end or even a middle, You can then tell tsset or xtset that your dates are weekly insofar as they are 7 days apart.

                              If you're obliged to use the week numbers or labels of some external authority or data provider, you can create numeric or string variables -- as above -- or value labels.

                              Servicing even all the common variations -- try Googling "ISO week" and then follow links -- could be hundreds of lines of code and a lengthy help file that many users would complain about as being too long or too confusing. Naturally, most people care about their own specific problem and want code and help that focus on that.

                              Note: If weeks end on Friday, then they start on Saturday. But there is still a nuance that may need attention. Weeks beginning on Saturdays are often indexed by Saturday; weeks ending on Fridays by Friday.
                              Last edited by Nick Cox; 01 Feb 2019, 02:33.

                              Comment

                              Working...
                              X