Announcement

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

  • biweekly time variable

    Hi, I would like to create a biweekly time variable, for example 2000m1q1, 2000m1q2,2000m2q1, 2000m2q2, etc
    I have tried but without sucessful.
    Maybe could you help me how to do it?
    Thanks in advance

  • #2
    "q" conventionally denotes quarters. Your question is ill-posed as it is difficult to define biweekly based on calendar months (as months do not have the same number of days). If you can impose the assumption that the first half of the month consists of days 1-15, then the following is a feasible solution. I use labmask from the Stata Journal below.

    Code:
    *TO INSTALL
    findit labmask
    Code:
    webuse sp500, clear
    tsset date
    gen present=1
    tsfill, full
    gen month= mofd(date)
    format month %tm
    gen half= cond(day(date)<=15, 1, 2)
    gen mlabel= string(month, "%tm")+"h"+string(half)
    egen wanted= group(month half)
    labmask wanted, values(mlabel)
    drop if missing(present)
    drop present
    Res.:

    Code:
    .
    
    . l date month half wanted in 1/50, sep(0)
    
         +--------------------------------------+
         |      date    month   half     wanted |
         |--------------------------------------|
      1. | 02jan2001   2001m1      1   2001m1h1 |
      2. | 03jan2001   2001m1      1   2001m1h1 |
      3. | 04jan2001   2001m1      1   2001m1h1 |
      4. | 05jan2001   2001m1      1   2001m1h1 |
      5. | 08jan2001   2001m1      1   2001m1h1 |
      6. | 09jan2001   2001m1      1   2001m1h1 |
      7. | 10jan2001   2001m1      1   2001m1h1 |
      8. | 11jan2001   2001m1      1   2001m1h1 |
      9. | 12jan2001   2001m1      1   2001m1h1 |
     10. | 16jan2001   2001m1      2   2001m1h2 |
     11. | 17jan2001   2001m1      2   2001m1h2 |
     12. | 18jan2001   2001m1      2   2001m1h2 |
     13. | 19jan2001   2001m1      2   2001m1h2 |
     14. | 22jan2001   2001m1      2   2001m1h2 |
     15. | 23jan2001   2001m1      2   2001m1h2 |
     16. | 24jan2001   2001m1      2   2001m1h2 |
     17. | 25jan2001   2001m1      2   2001m1h2 |
     18. | 26jan2001   2001m1      2   2001m1h2 |
     19. | 29jan2001   2001m1      2   2001m1h2 |
     20. | 30jan2001   2001m1      2   2001m1h2 |
     21. | 31jan2001   2001m1      2   2001m1h2 |
     22. | 01feb2001   2001m2      1   2001m2h1 |
     23. | 02feb2001   2001m2      1   2001m2h1 |
     24. | 05feb2001   2001m2      1   2001m2h1 |
     25. | 06feb2001   2001m2      1   2001m2h1 |
     26. | 07feb2001   2001m2      1   2001m2h1 |
     27. | 08feb2001   2001m2      1   2001m2h1 |
     28. | 09feb2001   2001m2      1   2001m2h1 |
     29. | 12feb2001   2001m2      1   2001m2h1 |
     30. | 13feb2001   2001m2      1   2001m2h1 |
     31. | 14feb2001   2001m2      1   2001m2h1 |
     32. | 15feb2001   2001m2      1   2001m2h1 |
     33. | 16feb2001   2001m2      2   2001m2h2 |
     34. | 20feb2001   2001m2      2   2001m2h2 |
     35. | 21feb2001   2001m2      2   2001m2h2 |
     36. | 22feb2001   2001m2      2   2001m2h2 |
     37. | 23feb2001   2001m2      2   2001m2h2 |
     38. | 26feb2001   2001m2      2   2001m2h2 |
     39. | 27feb2001   2001m2      2   2001m2h2 |
     40. | 28feb2001   2001m2      2   2001m2h2 |
     41. | 01mar2001   2001m3      1   2001m3h1 |
     42. | 02mar2001   2001m3      1   2001m3h1 |
     43. | 05mar2001   2001m3      1   2001m3h1 |
     44. | 06mar2001   2001m3      1   2001m3h1 |
     45. | 07mar2001   2001m3      1   2001m3h1 |
     46. | 08mar2001   2001m3      1   2001m3h1 |
     47. | 09mar2001   2001m3      1   2001m3h1 |
     48. | 12mar2001   2001m3      1   2001m3h1 |
     49. | 13mar2001   2001m3      1   2001m3h1 |
     50. | 14mar2001   2001m3      1   2001m3h1 |
         +--------------------------------------+
    Last edited by Andrew Musau; 29 Dec 2022, 14:26.

    Comment


    • #3
      What Andrew shows is a good start, but OP can present a data sample and say what he wants with respect to this data sample.

      Comment


      • #4
        The key question remains precisely what is wanted and why this is wanted. If what is wanted is a date variable that covers two-week periods, each precisely 14 days long, then manifestly such periods won't nest neatly in years, months, half-years or quarters, except occasionally.

        You need your own machinery. Here are some token examples whenever two-week periods start on Sundays (dow() returns 0).

        Code:
         
        . clear
        
        . set obs 31
        Number of observations (_N) was 0, now 31.
        
        . gen ddate = mdy(12, _n, 2022)
        
        . gen wanted1 = ceil(sum(dow(ddate) == 0) / 2)
        
        .
        
        . gen wanted2 = floor(sum(dow(ddate) == 0) / 2)
        
        . l , sepby(wanted1)
        
             +---------------------------+
             | ddate   wanted1   wanted2 |
             |---------------------------|
          1. | 22980         0         0 |
          2. | 22981         0         0 |
          3. | 22982         0         0 |
             |---------------------------|
          4. | 22983         1         0 |
          5. | 22984         1         0 |
          6. | 22985         1         0 |
          7. | 22986         1         0 |
          8. | 22987         1         0 |
          9. | 22988         1         0 |
         10. | 22989         1         0 |
         11. | 22990         1         1 |
         12. | 22991         1         1 |
         13. | 22992         1         1 |
         14. | 22993         1         1 |
         15. | 22994         1         1 |
         16. | 22995         1         1 |
         17. | 22996         1         1 |
             |---------------------------|
         18. | 22997         2         1 |
         19. | 22998         2         1 |
         20. | 22999         2         1 |
         21. | 23000         2         1 |
         22. | 23001         2         1 |
         23. | 23002         2         1 |
         24. | 23003         2         1 |
         25. | 23004         2         2 |
         26. | 23005         2         2 |
         27. | 23006         2         2 |
         28. | 23007         2         2 |
         29. | 23008         2         2 |
         30. | 23009         2         2 |
         31. | 23010         2         2 |
             +---------------------------+
        Easy variations and extensions.

        1. A different day of the week starts each 2 week period.

        2. Indexing each 14-day period by the day that starts it (or ends it). .

        Comment


        • #5
          Good morning, I would like to work with the following data. To do that I need to convert the date in stata format. My information is given twice a month. Please excuse me the information is in Spanish.
          Periodo disponible 1Q Ene 1988-1Q Dic 2022
          Periodicidad Quincenal
          Fecha Serie
          1Q Ene 1988 4.636980106457
          2Q Ene 1988 4.799511567983
          1Q Feb 1988 5.041704343994
          2Q Feb 1988 5.181864637259
          1Q Mar 1988 5.332375150997
          2Q Mar 1988 5.414721544451
          1Q Abr 1988 5.526642660681
          2Q Abr 1988 5.551240429252
          1Q May 1988 5.615513438571
          2Q May 1988 5.676704020750
          1Q Jun 1988 5.739709867584
          2Q Jun 1988 5.782874208633
          1Q Jul 1988 5.844765201147
          2Q Jul 1988 5.870150931344
          1Q Ago 1988 5.908970863318
          2Q Ago 1988 5.913717283058
          1Q Sep 1988 5.944982419239
          2Q Sep 1988 5.945298432777
          In stata I have the string variable... and to work with this information I have to change the format of the date in stata format.

          1Q Ene 1988 4.63698011
          2Q Ene 1988 4.79951157
          1Q Feb 1988 5.04170434
          2Q Feb 1988 5.18186464
          1Q Mar 1988 5.33237515
          2Q Mar 1988 5.41472154
          1Q Abr 1988 5.52664266
          2Q Abr 1988 5.55124043
          1Q May 1988 5.61551344
          2Q May 1988 5.67670402
          1Q Jun 1988 5.73970987
          2Q Jun 1988 5.78287421
          1Q Jul 1988 5.8447652
          2Q Jul 1988 5.87015093
          1Q Ago 1988 5.90897086
          Last edited by david pinchao; 30 Dec 2022, 10:11.

          Comment


          • #6
            Nothing in Stata dates corresponds to #5. You should just map your dates to successive integers and use the string values as value labels.

            Comment


            • #7
              Joro Kolev, Andrew Musau good afternoon, maybe do you know how to work with dates in #5?, in this case q means biweekly not quarter. The time serie correspond to mexican inflation . https://www.inegi.org.mx/app/tabulad...idrt=137&opc=t
              the xlsx file. Thanks in advance.
              Last edited by david pinchao; 30 Dec 2022, 14:32.

              Comment


              • #8
                A time variable is simply a set of integers with a unit representing the period between observations. So Nick's suggestion in #6 sounds good to me. Just make sure that there are no gaps, so that if "1Q Ene 1988" is mapped to 1, then even if "2Q Ene 1988" is missing, "1Q Feb 1988" is mapped to 3 and not to 2. Here is a start:

                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input str11 fecha float serie
                "1Q Ene 1988"  4.63698
                "2Q Ene 1988" 4.799511
                "1Q Feb 1988" 5.041704
                "2Q Feb 1988" 5.181865
                "1Q Mar 1988" 5.332375
                "2Q Mar 1988" 5.414721
                "1Q Abr 1988" 5.526643
                "2Q Abr 1988"  5.55124
                "1Q May 1988" 5.615513
                "2Q May 1988" 5.676704
                "1Q Jun 1988"  5.73971
                "2Q Jun 1988" 5.782874
                "1Q Jul 1988" 5.844765
                "2Q Jul 1988" 5.870151
                "1Q Ago 1988" 5.908971
                "2Q Ago 1988" 5.913717
                "1Q Sep 1988" 5.944983
                "2Q Sep 1988" 5.945298
                end
                
                gen year= substr(fecha,-4, 4)
                gen month= substr(fecha, 4, 3)
                replace month= "Jan" if month=="Ene"
                replace month="Apr" if month== "Abr"
                replace month="Aug" if month== "Ago"
                gen half= real(substr(fecha, 1, 1))
                gen ym= monthly(month+year, "MY")
                format ym %tm
                xtset half ym
                gen present=1
                tsfill, full
                sort ym half
                gen time=_n
                labmask time, values(fecha)
                drop if missing(present)
                drop present
                tsset time
                Then use the variable named "time" as you would with any other time variable.

                Res.:

                Code:
                . l, sep(0)
                
                     +---------------------------------------------------------------------+
                     |       fecha      serie   year   month   half       ym          time |
                     |---------------------------------------------------------------------|
                  1. | 1Q Ene 1988    4.63698   1988     Jan      1   1988m1   1Q Ene 1988 |
                  2. | 2Q Ene 1988   4.799511   1988     Jan      2   1988m1   2Q Ene 1988 |
                  3. | 1Q Feb 1988   5.041704   1988     Feb      1   1988m2   1Q Feb 1988 |
                  4. | 2Q Feb 1988   5.181865   1988     Feb      2   1988m2   2Q Feb 1988 |
                  5. | 1Q Mar 1988   5.332375   1988     Mar      1   1988m3   1Q Mar 1988 |
                  6. | 2Q Mar 1988   5.414721   1988     Mar      2   1988m3   2Q Mar 1988 |
                  7. | 1Q Abr 1988   5.526643   1988     Apr      1   1988m4   1Q Abr 1988 |
                  8. | 2Q Abr 1988    5.55124   1988     Apr      2   1988m4   2Q Abr 1988 |
                  9. | 1Q May 1988   5.615513   1988     May      1   1988m5   1Q May 1988 |
                 10. | 2Q May 1988   5.676704   1988     May      2   1988m5   2Q May 1988 |
                 11. | 1Q Jun 1988    5.73971   1988     Jun      1   1988m6   1Q Jun 1988 |
                 12. | 2Q Jun 1988   5.782874   1988     Jun      2   1988m6   2Q Jun 1988 |
                 13. | 1Q Jul 1988   5.844765   1988     Jul      1   1988m7   1Q Jul 1988 |
                 14. | 2Q Jul 1988   5.870151   1988     Jul      2   1988m7   2Q Jul 1988 |
                 15. | 1Q Ago 1988   5.908971   1988     Aug      1   1988m8   1Q Ago 1988 |
                 16. | 2Q Ago 1988   5.913717   1988     Aug      2   1988m8   2Q Ago 1988 |
                 17. | 1Q Sep 1988   5.944983   1988     Sep      1   1988m9   1Q Sep 1988 |
                 18. | 2Q Sep 1988   5.945298   1988     Sep      2   1988m9   2Q Sep 1988 |
                     +---------------------------------------------------------------------+
                
                .
                Last edited by Andrew Musau; 30 Dec 2022, 15:34.

                Comment


                • #9
                  Just to note that Q here presumably stands for quinzana, whose nearest English equivalent is fortnight, except that 15 != 14, and fortnights don’t nest in months.

                  Comment

                  Working...
                  X