Announcement

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

  • Generating Bi-weekly variable

    Hello All,

    Please consider the following data example:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str10 date str8 indicator float totalreturn byte(date2 month) int cal_year float(f_year ddate total_return_point wdate wdatef)
    "01-05-2024" "CVU.A"   -1.5151515  1  5 2024 2025 23497  -.015151515 23495 23495
    "02-05-2024" "CVU.A"   -1.1538461  2  5 2024 2025 23498  -.011538462 23495 23495
    "03-05-2024" "CVU.A"   -1.5564202  3  5 2024 2025 23499  -.015564202 23495 23495
    "06-05-2024" "CVU.A"     4.743083  6  5 2024 2025 23502    .04743083 23502 23502
    "07-05-2024" "CVU.A"     -.754717  7  5 2024 2025 23503   -.00754717 23502 23502
    "08-05-2024" "CVU.A"    -.3802281  8  5 2024 2025 23504  -.003802281 23502 23502
    "09-05-2024" "CVU.A"    1.1450381  9  5 2024 2025 23505    .01145038 23502 23502
    "10-05-2024" "CVU.A"    2.2641509 10  5 2024 2025 23506    .02264151 23502 23502
    "13-05-2024" "CVU.A"     .3690037 13  5 2024 2025 23509   .003690037 23509 23509
    "14-05-2024" "CVU.A"     .7352941 14  5 2024 2025 23510   .007352941 23509 23509
    "15-05-2024" "CVU.A"    4.0145984 15  5 2024 2025 23511    .04014598 23509 23509
    "16-05-2024" "CVU.A"    -5.964912 16  5 2024 2025 23512   -.05964912 23509 23509
    "17-05-2024" "CVU.A"   -4.1044774 17  5 2024 2025 23513   -.04104478 23509 23509
    "20-05-2024" "CVU.A"   -4.2801557 20  5 2024 2025 23516   -.04280156 23516 23516
    "21-05-2024" "CVU.A"   -1.6260163 21  5 2024 2025 23517  -.016260162 23516 23516
    "22-05-2024" "CVU.A"    -.8264463 22  5 2024 2025 23518  -.008264463 23516 23516
    "23-05-2024" "CVU.A"   -1.6666666 23  5 2024 2025 23519  -.016666666 23516 23516
    "24-05-2024" "CVU.A"   -1.2711865 24  5 2024 2025 23520  -.012711865 23516 23516
    "28-05-2024" "CVU.A"     .8583691 28  5 2024 2025 23524   .008583691 23523 23523
    "29-05-2024" "CVU.A"   -2.1276596 29  5 2024 2025 23525  -.021276595 23523 23523
    "30-05-2024" "CVU.A"     .4347826 30  5 2024 2025 23526   .004347826 23523 23523
    "31-05-2024" "CVU.A"    1.2987013 31  5 2024 2025 23527   .012987013 23523 23523
    "31-05-2024" "CVU.A"    1.2987013 31  5 2024 2025 23527   .012987013 23523 23523
    "01-05-2024" "EBF.N"      1.20603  1  5 2024 2025 23497     .0120603 23495 23495
    "02-05-2024" "EBF.N"     .6454816  2  5 2024 2025 23498   .006454817 23495 23495
    "03-05-2024" "EBF.N"   -.04933399  3  5 2024 2025 23499 -.0004933399 23495 23495
    "06-05-2024" "EBF.N"     .3948667  6  5 2024 2025 23502  .0039486676 23502 23502
    "07-05-2024" "EBF.N"    .19665684  7  5 2024 2025 23503  .0019665684 23502 23502
    "08-05-2024" "EBF.N"     1.030422  8  5 2024 2025 23504    .01030422 23502 23502
    "09-05-2024" "EBF.N"     .5828072  9  5 2024 2025 23505   .005828072 23502 23502
    "10-05-2024" "EBF.N"     -.965717 10  5 2024 2025 23506   -.00965717 23502 23502
    "13-05-2024" "EBF.N"     .2437835 13  5 2024 2025 23509   .002437835 23509 23509
    "14-05-2024" "EBF.N"     .4377432 14  5 2024 2025 23510  .0043774317 23509 23509
    "15-05-2024" "EBF.N"    -.3389831 15  5 2024 2025 23511  -.003389831 23509 23509
    "16-05-2024" "EBF.N"     1.068999 16  5 2024 2025 23512    .01068999 23509 23509
    "17-05-2024" "EBF.N"    -.7211539 17  5 2024 2025 23513  -.007211538 23509 23509
    "20-05-2024" "EBF.N"    -.6779661 20  5 2024 2025 23516  -.006779661 23516 23516
    "21-05-2024" "EBF.N"    1.2189176 21  5 2024 2025 23517   .012189176 23516 23516
    "22-05-2024" "EBF.N"    .19267823 22  5 2024 2025 23518  .0019267823 23516 23516
    "23-05-2024" "EBF.N"    -.5769231 23  5 2024 2025 23519  -.005769231 23516 23516
    "24-05-2024" "EBF.N"     .7253385 24  5 2024 2025 23520   .007253385 23516 23516
    "28-05-2024" "EBF.N"    -.5760922 28  5 2024 2025 23524  -.005760922 23523 23523
    "29-05-2024" "EBF.N"    -.6760019 29  5 2024 2025 23525  -.006760019 23523 23523
    "30-05-2024" "EBF.N"     .7292173 30  5 2024 2025 23526   .007292173 23523 23523
    "31-05-2024" "EBF.N"     1.496139 31  5 2024 2025 23527    .01496139 23523 23523
    "31-05-2024" "EBF.N"     1.496139 31  5 2024 2025 23527    .01496139 23523 23523
    "01-05-2024" "ECL.N"   -.51735574  1  5 2024 2025 23497  -.005173557 23495 23495
    "02-05-2024" "ECL.N"     .7911814  2  5 2024 2025 23498   .007911814 23495 23495
    "03-05-2024" "ECL.N"     .3263362  3  5 2024 2025 23499   .003263362 23495 23495
    "06-05-2024" "ECL.N"     .6373627  6  5 2024 2025 23502   .006373627 23502 23502
    "07-05-2024" "ECL.N"     1.734003  7  5 2024 2025 23503    .01734003 23502 23502
    "08-05-2024" "ECL.N"    .13309291  8  5 2024 2025 23504   .001330929 23502 23502
    "09-05-2024" "ECL.N"    .15006647  9  5 2024 2025 23505  .0015006646 23502 23502
    "10-05-2024" "ECL.N"   -.02568713 10  5 2024 2025 23506 -.0002568713 23502 23502
    "13-05-2024" "ECL.N"     -.757965 13  5 2024 2025 23509   -.00757965 23509 23509
    "14-05-2024" "ECL.N"    .06472492 14  5 2024 2025 23510  .0006472492 23509 23509
    "15-05-2024" "ECL.N"     .4527814 15  5 2024 2025 23511  .0045278138 23509 23509
    "16-05-2024" "ECL.N"   -.13736853 16  5 2024 2025 23512 -.0013736853 23509 23509
    "17-05-2024" "ECL.N"     .4427632 17  5 2024 2025 23513   .004427632 23509 23509
    "20-05-2024" "ECL.N"     .4408114 20  5 2024 2025 23516   .004408114 23516 23516
    "21-05-2024" "ECL.N"    .13208914 21  5 2024 2025 23517  .0013208914 23516 23516
    "22-05-2024" "ECL.N"    -.5531915 22  5 2024 2025 23518  -.005531915 23516 23516
    "23-05-2024" "ECL.N"   -.24390244 23  5 2024 2025 23519 -.0024390244 23516 23516
    "24-05-2024" "ECL.N"     .5061554 24  5 2024 2025 23520   .005061554 23516 23516
    "28-05-2024" "ECL.N"    -2.308907 28  5 2024 2025 23524   -.02308907 23523 23523
    "29-05-2024" "ECL.N"   -1.2101355 29  5 2024 2025 23525  -.012101355 23523 23523
    "30-05-2024" "ECL.N"    1.1365144 30  5 2024 2025 23526   .011365145 23523 23523
    "31-05-2024" "ECL.N"    1.5303892 31  5 2024 2025 23527   .015303892 23523 23523
    "31-05-2024" "ECL.N"    1.5303892 31  5 2024 2025 23527   .015303892 23523 23523
    "01-05-2024" "EDUC.OQ"  .46296296  1  5 2024 2025 23497  .0046296297 23495 23495
    "02-05-2024" "EDUC.OQ"  -5.990783  2  5 2024 2025 23498   -.05990783 23495 23495
    "03-05-2024" "EDUC.OQ"  -6.862745  3  5 2024 2025 23499  -.068627454 23495 23495
    "06-05-2024" "EDUC.OQ"  -4.736842  6  5 2024 2025 23502   -.04736842 23502 23502
    "07-05-2024" "EDUC.OQ"  8.2872925  7  5 2024 2025 23503    .08287293 23502 23502
    "08-05-2024" "EDUC.OQ"   5.612245  8  5 2024 2025 23504    .05612245 23502 23502
    "09-05-2024" "EDUC.OQ"   4.347826  9  5 2024 2025 23505    .04347826 23502 23502
    "10-05-2024" "EDUC.OQ"  4.6296296 10  5 2024 2025 23506     .0462963 23502 23502
    "13-05-2024" "EDUC.OQ"  -4.424779 13  5 2024 2025 23509   -.04424779 23509 23509
    "14-05-2024" "EDUC.OQ"  .46296296 14  5 2024 2025 23510  .0046296297 23509 23509
    "15-05-2024" "EDUC.OQ"    .921659 15  5 2024 2025 23511    .00921659 23509 23509
    "16-05-2024" "EDUC.OQ"  -8.219178 16  5 2024 2025 23512   -.08219178 23509 23509
    "17-05-2024" "EDUC.OQ"   .9950249 17  5 2024 2025 23513   .009950249 23509 23509
    "20-05-2024" "EDUC.OQ"   2.955665 20  5 2024 2025 23516    .02955665 23516 23516
    "21-05-2024" "EDUC.OQ"   5.263158 21  5 2024 2025 23517    .05263158 23516 23516
    "22-05-2024" "EDUC.OQ"  -9.090909 22  5 2024 2025 23518    -.0909091 23516 23516
    "23-05-2024" "EDUC.OQ"       -1.5 23  5 2024 2025 23519        -.015 23516 23516
    "24-05-2024" "EDUC.OQ"  -2.538071 24  5 2024 2025 23520   -.02538071 23516 23516
    "28-05-2024" "EDUC.OQ"   3.385417 28  5 2024 2025 23524   .033854168 23523 23523
    "29-05-2024" "EDUC.OQ"  -2.770781 29  5 2024 2025 23525   -.02770781 23523 23523
    "30-05-2024" "EDUC.OQ"  -3.626943 30  5 2024 2025 23526   -.03626943 23523 23523
    "31-05-2024" "EDUC.OQ"  .53763443 31  5 2024 2025 23527   .005376345 23523 23523
    "31-05-2024" "EDUC.OQ"  .53763443 31  5 2024 2025 23527   .005376345 23523 23523
    "20-12-2023" "ELGT.PK"          0 20 12 2023 2024 23364            0 23362 23362
    "20-12-2023" "ELGT.PK"          0 20 12 2023 2024 23364            0 23362 23362
    "20-12-2023" "ELGT.PK"          0 20 12 2023 2024 23364            0 23362 23362
    "20-12-2023" "ELGT.PK"          0 20 12 2023 2024 23364            0 23362 23362
    "20-12-2023" "ELGT.PK"          0 20 12 2023 2024 23364            0 23362 23362
    "20-12-2023" "ELGT.PK"          0 20 12 2023 2024 23364            0 23362 23362
    "20-12-2023" "ELGT.PK"          0 20 12 2023 2024 23364            0 23362 23362
    "20-12-2023" "ELGT.PK"          0 20 12 2023 2024 23364            0 23362 23362
    end
    format %td ddate
    format %td wdatef

    I have generated a variable (wdatef) for each week, starting from Monday (April 29) to Friday (May 03), using the following command:
    Code:
    gen wdate = ddate - dow(ddate) +1
    clonevar wdatef = wdate
    format wdatef %td
    Now, I want to generate a new bi-weekly variable, i.e., from Monday (April 29) to Friday (May 10), Monday (May 13) to Friday (May 24) and so on.

    Can anyone please help me with this? Thank you in advance.

  • #2
    To me, it's most intuitive to work with the dates in their weekly format. So something like this:

    Code:
    gen datew = wofd(ddate) 
    gen datebw = datew - 1 + mod(datew, 2)
    
    replace datew = dofw(datew)
    replace datebw = dofw(datebw)
    
    format %td datew datebw

    Comment


    • #3
      Thank you, Hemanahu. The code worked perfectly.

      I have one small query: the code gives the Sunday's date in the datebw. Can I get Monday's date instead of Sunday's date?
      Last edited by Nihar Singh; 03 Jun 2024, 08:13.

      Comment


      • #4
        I have to disagree here. Stata weeks don't map onto weeks in any other sense. Check carefully results for days 365 (and 366 if present).

        If 29 April 2024 is in some sense the origin, we can work like this:

        Code:
        clear 
        set obs 41
        gen date = 23475 + _n
        format date %td
        gen w1 = ceil((date - 23494)/7)
        gen w2 = ceil((date - 23494)/14)
        gen dow = dow(date)
        l if inrange(dow, 1, 5), sepby(w1)
        
        
             +---------------------------+
             |      date   w1   w2   dow |
             |---------------------------|
          1. | 10apr2024   -2   -1     3 |
          2. | 11apr2024   -2   -1     4 |
          3. | 12apr2024   -2   -1     5 |
             |---------------------------|
          6. | 15apr2024   -1    0     1 |
          7. | 16apr2024   -1    0     2 |
          8. | 17apr2024   -1    0     3 |
          9. | 18apr2024   -1    0     4 |
         10. | 19apr2024   -1    0     5 |
             |---------------------------|
         13. | 22apr2024    0    0     1 |
         14. | 23apr2024    0    0     2 |
         15. | 24apr2024    0    0     3 |
         16. | 25apr2024    0    0     4 |
         17. | 26apr2024    0    0     5 |
             |---------------------------|
         20. | 29apr2024    1    1     1 |
         21. | 30apr2024    1    1     2 |
         22. | 01may2024    1    1     3 |
         23. | 02may2024    1    1     4 |
         24. | 03may2024    1    1     5 |
             |---------------------------|
         27. | 06may2024    2    1     1 |
         28. | 07may2024    2    1     2 |
         29. | 08may2024    2    1     3 |
         30. | 09may2024    2    1     4 |
         31. | 10may2024    2    1     5 |
             |---------------------------|
         34. | 13may2024    3    2     1 |
         35. | 14may2024    3    2     2 |
         36. | 15may2024    3    2     3 |
         37. | 16may2024    3    2     4 |
         38. | 17may2024    3    2     5 |
             |---------------------------|
         41. | 20may2024    4    2     1 |
             +---------------------------+

        Comment


        • #5
          Please ignore the code in #2. It appears to be unreliable.

          What is puzzling to me is that the dow() function appears to give different results for different weeks. My understanding was that when you convert a weekly date to a daily date using dow(), the missing component of the weekly date, i.e. the day, would be set to its default, which is 1 (this is what help datetime explicitly states). This should mean Monday. However in your data extract, I get Mondays for some weeks (e.g. Mon, 27 May 2024), but Sundays for other weeks (e.g. Sun, 10 Dec 2023). I would appreciate it if someone can explain what is going on.

          Comment


          • #6
            The starting point here is that for Stata week 1 in any given year always starts on 1 January, week 2 always starts on 8 January, etc., and week 52 has either 8 or 9 days depending on whether the year is not or is leap.

            Although Stata's dow() function always returns 0 to 6 for Sunday to Saturday, that is independent of Stata's definition of week in the sense that the daily date alone determines the answer.

            Other way round, dofw() always returns the first day of any week, in Stata's sense, which correspondingly may be any day of the week, but is constant within any year.

            For 2023, it was 0 or Sunday. For 2024 it is 1 or Monday.


            Code:
            clear
            set obs 62
            gen date = mdy(12,31,2022) + _n in 1/31 
            replace date = mdy(12,31,2023) + (_n - 31) in 32/62 
            format date %td 
            
            gen year = year(date)
            
            gen dow = dow(date)
            gen week = wofd(date)
            
            format week %tw 
            
            gen DATE = dofw(week)
            format DATE %td 
            
            gen DOW = dow(DATE)
            
            l , sepby(week)
            
                 +---------------------------------------------------+
                 |      date   year   dow     week        DATE   DOW |
                 |---------------------------------------------------|
              1. | 01jan2023   2023     0   2023w1   01jan2023     0 |
              2. | 02jan2023   2023     1   2023w1   01jan2023     0 |
              3. | 03jan2023   2023     2   2023w1   01jan2023     0 |
              4. | 04jan2023   2023     3   2023w1   01jan2023     0 |
              5. | 05jan2023   2023     4   2023w1   01jan2023     0 |
              6. | 06jan2023   2023     5   2023w1   01jan2023     0 |
              7. | 07jan2023   2023     6   2023w1   01jan2023     0 |
                 |---------------------------------------------------|
              8. | 08jan2023   2023     0   2023w2   08jan2023     0 |
              9. | 09jan2023   2023     1   2023w2   08jan2023     0 |
             10. | 10jan2023   2023     2   2023w2   08jan2023     0 |
             11. | 11jan2023   2023     3   2023w2   08jan2023     0 |
             12. | 12jan2023   2023     4   2023w2   08jan2023     0 |
             13. | 13jan2023   2023     5   2023w2   08jan2023     0 |
             14. | 14jan2023   2023     6   2023w2   08jan2023     0 |
                 |---------------------------------------------------|
             15. | 15jan2023   2023     0   2023w3   15jan2023     0 |
             16. | 16jan2023   2023     1   2023w3   15jan2023     0 |
             17. | 17jan2023   2023     2   2023w3   15jan2023     0 |
             18. | 18jan2023   2023     3   2023w3   15jan2023     0 |
             19. | 19jan2023   2023     4   2023w3   15jan2023     0 |
             20. | 20jan2023   2023     5   2023w3   15jan2023     0 |
             21. | 21jan2023   2023     6   2023w3   15jan2023     0 |
                 |---------------------------------------------------|
             22. | 22jan2023   2023     0   2023w4   22jan2023     0 |
             23. | 23jan2023   2023     1   2023w4   22jan2023     0 |
             24. | 24jan2023   2023     2   2023w4   22jan2023     0 |
             25. | 25jan2023   2023     3   2023w4   22jan2023     0 |
             26. | 26jan2023   2023     4   2023w4   22jan2023     0 |
             27. | 27jan2023   2023     5   2023w4   22jan2023     0 |
             28. | 28jan2023   2023     6   2023w4   22jan2023     0 |
                 |---------------------------------------------------|
             29. | 29jan2023   2023     0   2023w5   29jan2023     0 |
             30. | 30jan2023   2023     1   2023w5   29jan2023     0 |
             31. | 31jan2023   2023     2   2023w5   29jan2023     0 |
                 |---------------------------------------------------|
             32. | 01jan2024   2024     1   2024w1   01jan2024     1 |
             33. | 02jan2024   2024     2   2024w1   01jan2024     1 |
             34. | 03jan2024   2024     3   2024w1   01jan2024     1 |
             35. | 04jan2024   2024     4   2024w1   01jan2024     1 |
             36. | 05jan2024   2024     5   2024w1   01jan2024     1 |
             37. | 06jan2024   2024     6   2024w1   01jan2024     1 |
             38. | 07jan2024   2024     0   2024w1   01jan2024     1 |
                 |---------------------------------------------------|
             39. | 08jan2024   2024     1   2024w2   08jan2024     1 |
             40. | 09jan2024   2024     2   2024w2   08jan2024     1 |
             41. | 10jan2024   2024     3   2024w2   08jan2024     1 |
             42. | 11jan2024   2024     4   2024w2   08jan2024     1 |
             43. | 12jan2024   2024     5   2024w2   08jan2024     1 |
             44. | 13jan2024   2024     6   2024w2   08jan2024     1 |
             45. | 14jan2024   2024     0   2024w2   08jan2024     1 |
                 |---------------------------------------------------|
             46. | 15jan2024   2024     1   2024w3   15jan2024     1 |
             47. | 16jan2024   2024     2   2024w3   15jan2024     1 |
             48. | 17jan2024   2024     3   2024w3   15jan2024     1 |
             49. | 18jan2024   2024     4   2024w3   15jan2024     1 |
             50. | 19jan2024   2024     5   2024w3   15jan2024     1 |
             51. | 20jan2024   2024     6   2024w3   15jan2024     1 |
             52. | 21jan2024   2024     0   2024w3   15jan2024     1 |
                 |---------------------------------------------------|
             53. | 22jan2024   2024     1   2024w4   22jan2024     1 |
             54. | 23jan2024   2024     2   2024w4   22jan2024     1 |
             55. | 24jan2024   2024     3   2024w4   22jan2024     1 |
             56. | 25jan2024   2024     4   2024w4   22jan2024     1 |
             57. | 26jan2024   2024     5   2024w4   22jan2024     1 |
             58. | 27jan2024   2024     6   2024w4   22jan2024     1 |
             59. | 28jan2024   2024     0   2024w4   22jan2024     1 |
                 |---------------------------------------------------|
             60. | 29jan2024   2024     1   2024w5   29jan2024     1 |
             61. | 30jan2024   2024     2   2024w5   29jan2024     1 |
             62. | 31jan2024   2024     3   2024w5   29jan2024     1 |
                 +---------------------------------------------------+
            .


            Comment


            • #7
              On what day of the week does 1 January fall? Here are two ways to get at that.

              Code:
              . mata : (2023::2040), dow(dofw(yw((2023::2040), 1)))
                         1      2
                   +---------------+
                 1 |  2023      0  |
                 2 |  2024      1  |
                 3 |  2025      3  |
                 4 |  2026      4  |
                 5 |  2027      5  |
                 6 |  2028      6  |
                 7 |  2029      1  |
                 8 |  2030      2  |
                 9 |  2031      3  |
                10 |  2032      4  |
                11 |  2033      6  |
                12 |  2034      0  |
                13 |  2035      1  |
                14 |  2036      2  |
                15 |  2037      4  |
                16 |  2038      5  |
                17 |  2039      6  |
                18 |  2040      0  |
                   +---------------+
              
              . mata : (2023::2040), dow(mdy(1, 1, (2023::2040)))
                         1      2
                   +---------------+
                 1 |  2023      0  |
                 2 |  2024      1  |
                 3 |  2025      3  |
                 4 |  2026      4  |
                 5 |  2027      5  |
                 6 |  2028      6  |
                 7 |  2029      1  |
                 8 |  2030      2  |
                 9 |  2031      3  |
                10 |  2032      4  |
                11 |  2033      6  |
                12 |  2034      0  |
                13 |  2035      1  |
                14 |  2036      2  |
                15 |  2037      4  |
                16 |  2038      5  |
                17 |  2039      6  |
                18 |  2040      0  |
                   +---------------+
              .

              Comment


              • #8
                Thank you, Nick, for your comments #4, 6 &7.

                Can you please suggest what changes need to be made in #2 to get a bi-weekly variable?
                Last edited by Nihar Singh; 04 Jun 2024, 00:08.

                Comment


                • #9
                  No; I won't do that. I think you're still missing the main point, which is subtle and has confused many smart users of Stata, sometimes because they won't read or prefer not to believe all of what is documented. Also, #4 stands as a solution.

                  You want weeks to start on Mondays, and to combine them in twos, as well. Stata's definition of week isn't consistent with that as a Stata week can start on any day of the week, which is not what you want.

                  So, functions based on Stata's definition of week such as wofd() and dofw() at best give the right answer, or a sensible-looking result, only by accident. I have to guess that Hemanshu Kumar was confused by this.

                  One of various subtleties here, as already mentioned, is that Stata's dow() function is independent of Stata's definition of weeks. If people don't read all of what is documented about weeks, there is still scope for confusion, because the results 0 for Sunday, ..., 6 for Saturday may lead people to believe that Stata weeks start on Sundays, which is one of several definitions of the week that can be found outside Stata.

                  Backing up to #1, you start out saying (or least implying) that your weeks and biweeks should start on 29 April 2024 yet in your data example you also have 20 December 2023. That's not a problem in itself: choosing the week starting 29 April 2024 as week 1 just means that earlier weeks have negative or zero indexes.

                  If you want an earlier start, choose any Sunday other than 23494 in a variant on the code in #4.

                  Alternatively, it can be practical to index or label each week or biweek by the Monday that starts it, but much depends on what you want to do next.

                  Comment


                  • #10
                    Nick, I want to calculate the weekly returns compounded from daily returns. For that, I want to create a bi-weekly variable starting from Wednesday to Tuesday. My dataset begins on Monday, January 1, 1990, and will be one according to #4.

                    "Backing up to #1, you start out saying (or least implying) that your weeks and weeks should start on 29 April 2024, yet in your data example, you also have 20 December 2023."

                    I have the dataset from January 1. 1990. I may have forgotten to sort the data before creating the data example; that may be why the example is 20 December 2023.

                    Comment


                    • #11
                      You have changed your question so far as I can see. I see nothing in this thread about weeks starting on Wednesday. But you now have all the principles explained, so you should be able to work out what you want that is different.

                      So far as I can see weeks beginning on Wednesday just require using a Tuesday rather than a Sunday in the code.

                      Here is some token code with a sandbox dataset. There are at least two possibilities, using your own counter for weeks and biweeks, and using the first day of each period (in conjunction with delta(7) or delta(14) if you also use tsset or xtset).


                      Code:
                      clear
                      set obs 62
                      gen date = mdy(12,31,1989) + _n in 1/31
                      replace date = mdy(12,31,2023) + (_n - 31) in 32/62 
                      format date %td 
                      
                      gen year = year(date)
                      
                      gen dow = dow(date)
                      
                      gen w1 = floor((date - mdy(12,27,1989))/7) 
                      gen w2 = ceil(w1/2)
                      
                      gen d1 = mdy(12, 27, 1989) + 7 * w1
                      gen d2 = mdy(12, 20, 1989) + 14 * ceil(w1/2) 
                      
                      format d1 d2 %td 
                      
                      l, sepby(w1)
                      
                           +-------------------------------------------------------------+
                           |      date   year   dow     w1    w2          d1          d2 |
                           |-------------------------------------------------------------|
                        1. | 01jan1990   1990     1      0     0   27dec1989   20dec1989 |
                        2. | 02jan1990   1990     2      0     0   27dec1989   20dec1989 |
                           |-------------------------------------------------------------|
                        3. | 03jan1990   1990     3      1     1   03jan1990   03jan1990 |
                        4. | 04jan1990   1990     4      1     1   03jan1990   03jan1990 |
                        5. | 05jan1990   1990     5      1     1   03jan1990   03jan1990 |
                        6. | 06jan1990   1990     6      1     1   03jan1990   03jan1990 |
                        7. | 07jan1990   1990     0      1     1   03jan1990   03jan1990 |
                        8. | 08jan1990   1990     1      1     1   03jan1990   03jan1990 |
                        9. | 09jan1990   1990     2      1     1   03jan1990   03jan1990 |
                           |-------------------------------------------------------------|
                       10. | 10jan1990   1990     3      2     1   10jan1990   03jan1990 |
                       11. | 11jan1990   1990     4      2     1   10jan1990   03jan1990 |
                       12. | 12jan1990   1990     5      2     1   10jan1990   03jan1990 |
                       13. | 13jan1990   1990     6      2     1   10jan1990   03jan1990 |
                       14. | 14jan1990   1990     0      2     1   10jan1990   03jan1990 |
                       15. | 15jan1990   1990     1      2     1   10jan1990   03jan1990 |
                       16. | 16jan1990   1990     2      2     1   10jan1990   03jan1990 |
                           |-------------------------------------------------------------|
                       17. | 17jan1990   1990     3      3     2   17jan1990   17jan1990 |
                       18. | 18jan1990   1990     4      3     2   17jan1990   17jan1990 |
                       19. | 19jan1990   1990     5      3     2   17jan1990   17jan1990 |
                       20. | 20jan1990   1990     6      3     2   17jan1990   17jan1990 |
                       21. | 21jan1990   1990     0      3     2   17jan1990   17jan1990 |
                       22. | 22jan1990   1990     1      3     2   17jan1990   17jan1990 |
                       23. | 23jan1990   1990     2      3     2   17jan1990   17jan1990 |
                           |-------------------------------------------------------------|
                       24. | 24jan1990   1990     3      4     2   24jan1990   17jan1990 |
                       25. | 25jan1990   1990     4      4     2   24jan1990   17jan1990 |
                       26. | 26jan1990   1990     5      4     2   24jan1990   17jan1990 |
                       27. | 27jan1990   1990     6      4     2   24jan1990   17jan1990 |
                       28. | 28jan1990   1990     0      4     2   24jan1990   17jan1990 |
                       29. | 29jan1990   1990     1      4     2   24jan1990   17jan1990 |
                       30. | 30jan1990   1990     2      4     2   24jan1990   17jan1990 |
                           |-------------------------------------------------------------|
                       31. | 31jan1990   1990     3      5     3   31jan1990   31jan1990 |
                           |-------------------------------------------------------------|
                       32. | 01jan2024   2024     1   1774   887   27dec2023   20dec2023 |
                       33. | 02jan2024   2024     2   1774   887   27dec2023   20dec2023 |
                           |-------------------------------------------------------------|
                       34. | 03jan2024   2024     3   1775   888   03jan2024   03jan2024 |
                       35. | 04jan2024   2024     4   1775   888   03jan2024   03jan2024 |
                       36. | 05jan2024   2024     5   1775   888   03jan2024   03jan2024 |
                       37. | 06jan2024   2024     6   1775   888   03jan2024   03jan2024 |
                       38. | 07jan2024   2024     0   1775   888   03jan2024   03jan2024 |
                       39. | 08jan2024   2024     1   1775   888   03jan2024   03jan2024 |
                       40. | 09jan2024   2024     2   1775   888   03jan2024   03jan2024 |
                           |-------------------------------------------------------------|
                       41. | 10jan2024   2024     3   1776   888   10jan2024   03jan2024 |
                       42. | 11jan2024   2024     4   1776   888   10jan2024   03jan2024 |
                       43. | 12jan2024   2024     5   1776   888   10jan2024   03jan2024 |
                       44. | 13jan2024   2024     6   1776   888   10jan2024   03jan2024 |
                       45. | 14jan2024   2024     0   1776   888   10jan2024   03jan2024 |
                       46. | 15jan2024   2024     1   1776   888   10jan2024   03jan2024 |
                       47. | 16jan2024   2024     2   1776   888   10jan2024   03jan2024 |
                           |-------------------------------------------------------------|
                       48. | 17jan2024   2024     3   1777   889   17jan2024   17jan2024 |
                       49. | 18jan2024   2024     4   1777   889   17jan2024   17jan2024 |
                       50. | 19jan2024   2024     5   1777   889   17jan2024   17jan2024 |
                       51. | 20jan2024   2024     6   1777   889   17jan2024   17jan2024 |
                       52. | 21jan2024   2024     0   1777   889   17jan2024   17jan2024 |
                       53. | 22jan2024   2024     1   1777   889   17jan2024   17jan2024 |
                       54. | 23jan2024   2024     2   1777   889   17jan2024   17jan2024 |
                           |-------------------------------------------------------------|
                       55. | 24jan2024   2024     3   1778   889   24jan2024   17jan2024 |
                       56. | 25jan2024   2024     4   1778   889   24jan2024   17jan2024 |
                       57. | 26jan2024   2024     5   1778   889   24jan2024   17jan2024 |
                       58. | 27jan2024   2024     6   1778   889   24jan2024   17jan2024 |
                       59. | 28jan2024   2024     0   1778   889   24jan2024   17jan2024 |
                       60. | 29jan2024   2024     1   1778   889   24jan2024   17jan2024 |
                       61. | 30jan2024   2024     2   1778   889   24jan2024   17jan2024 |
                           |-------------------------------------------------------------|
                       62. | 31jan2024   2024     3   1779   890   31jan2024   31jan2024 |
                           +-------------------------------------------------------------+
                      Code:
                      
                      


                      Comment


                      • #12
                        Yes, Nick, I changed the question because I thought calculating returns for Wednesdays would be more comprehensive.

                        Thank you for the codes and great explanation.

                        Yes, while using the tsset command, I will use delta (7) or delta (14).

                        Comment

                        Working...
                        X