Announcement

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

  • Does -numdate- support week 53?

    I use the package -numdate- to generate a numeric weekly date variable from an existing string variable with information on year and week, using the following command:

    numdate weekly newvar=oldvar, pattern(YW)

    The command works fine for all week numbers exept week 53:

    oldvar newvar
    2020-48 2020w48
    2020-49 2020w49
    2020-50 2020w50
    2020-51 2020w51
    2020-52 2020w52
    2020-53 .
    2021-01 2021w1
    2021-02 2021w2



    Is there something I can do to make this command also work for week 53?

  • #2
    For Stata, weeks of the year always start on January 1st, and any year always has exactly 52 weeks: the last week may have 8 or 9 days -- the latter if it is a leap year like 2020. So week 53 is not legal in Stata's interpretation.

    Nick Cox has explained this here.

    If you are not using this convention, the missing value for week 53 is merely a symptom -- your problem is far more extensive. E.g. Monday, January 6th is in 2020 week 1 as per Stata, but if your weeks start on Sundays, this may very well be 2020 week 2 in your calendar.
    Last edited by Hemanshu Kumar; 21 Jul 2025, 08:13.

    Comment


    • #3
      Hemanshu Kumar is entirely correct. The point of numdate is entirely to provide a different implementation of Stata's facilities for generation of date variables, but there is no intent whatsoever to extend, still less to subvert, Stata's rules.

      If you want week 53, you can only be trying to implement a different definition of week.

      https://journals.sagepub.com/doi/pdf...6867X251341416 contains several references on weeks.

      Unless you are following some predefined set of rules -- say for epiweeks -- it seems to me that most uses of weeks call for handling in terms of the daily dates that define each week, say the first or last day of each week.

      From Stata's point of view an argument of 53 for week of year is as invalid as a result of 53 for week of year would be.
      Last edited by Nick Cox; 21 Jul 2025, 08:24.

      Comment


      • #4
        Note that numdate is from SSC (FAQ Advice #12). As #2 explains, Stata weekly dates range from week 1 to week 52 for any given year. See

        Code:
        help f_weekly
        where you have

        Range: e_w dates 0100w1 to 9999w52 (integers -96,720 to 418,079) or missing
        But let's take a step back and consider what a weekly date actually is. Weekly dates are just integers representing the number of weeks from some specified point in time. So, if you want to use the ISO calendar, you can generate your own weekly date variable. Assuming that the week numbers within years are exhaustive, and that oldvar in your example is a string variable, one way to do it is as follows:
        Code:
        clear
        input str10 oldvar
        "2020-48" 
        "2020-49" 
        "2020-50"
        "2020-51"
        "2020-52"
        "2020-53"
        "2021-01"
        "2021-02"
        end
        
        gen year=real(substr(oldvar, 1, 4))
        gen week= real(substr(oldvar, -2, 2))
        egen wdate= group(year week), label
        l
        tsset wdate
        If weeks are not exhaustive within years, you need to tweak the code to ensure that this is the case.

        Res.:

        Code:
        . l
        
             +---------------------------------+
             |  oldvar   year   week     wdate |
             |---------------------------------|
          1. | 2020-48   2020     48   2020 48 |
          2. | 2020-49   2020     49   2020 49 |
          3. | 2020-50   2020     50   2020 50 |
          4. | 2020-51   2020     51   2020 51 |
          5. | 2020-52   2020     52   2020 52 |
             |---------------------------------|
          6. | 2020-53   2020     53   2020 53 |
          7. | 2021-01   2021      1    2021 1 |
          8. | 2021-02   2021      2    2021 2 |
             +---------------------------------+
        
        . tsset wdate
        
        Time variable: wdate, 1 to 8
                Delta: 1 unit
        
        .

        Comment


        • #5
          I guess the question here should be -- why do you even need Stata's weekly dates?

          If for instance, you are just trying to make a simple graph, you don't need Stata's weekly dates, you just need a numeric variable that increases by week. Using encode is a simple way out.

          Here is an illustration:
          Code:
          clear
          input str7 oldvar yvar
          2020-48  5.5
          2020-49  6.3
          2020-50  7.7
          2020-51  6.8
          2020-52  7.2
          2020-53  5.9
          2021-01  8.1
          2021-02  8.6
          end
          
          gen labvar = subinstr(oldvar, "-", "w", 1)
          encode labvar, gen(newvar)
          drop labvar
          
          twoway connected yvar newvar, xlabel(#10, val) xtitle("Week")
          which produces:
          Click image for larger version

Name:	Screenshot 2025-07-21 at 8.05.01 PM.png
Views:	1
Size:	148.7 KB
ID:	1780096




          The numeric variable newvar above can also serve nicely in regressions where, say, you want week fixed effects.

          Edit: crossed with #4, which offers another solution using egen .. group.
          Last edited by Hemanshu Kumar; 21 Jul 2025, 08:40.

          Comment


          • #6
            Thank you, everyone. Very interesting. As Hemannshu Kumar suspects, I'm just trying to make a simple graph, so his suggestion involving -encode- will work just fine!

            Comment


            • #7
              As emphasised in #4, all simple codings to integers -- whether use egen, group() or encode -- depend on there being no gaps.

              Comment

              Working...
              X