Announcement

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

  • Python week-year to Stata week year

    I have data that I collected in python, exported to csv and I am now using it in Stata. One of my rows represents week_year but since this was created in Python, it shows up as 00-2022, etc. I am not sure how to convert it to stata format (2022w1).

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str4 id float degree_centrality str7 node_week_year1
    "505"  .02919708 "00-2022"
    "978"  .02919708 "00-2022"
    "394"  .05109489 "00-2022"
    "630"  .02919708 "00-2022"
    "93"   .08029197 "00-2022"
    "84"   .02919708 "00-2022"
    "20"   .23357664 "00-2022"
    "277"  .02919708 "00-2022"
    "2400" .02919708 "00-2022"
    "1559" .02919708 "00-2022"
    "166"  .02919708 "00-2022"
    "982"  .02919708 "00-2022"
    "1806" .05839416 "00-2022"
    "519"  .04379562 "00-2022"
    "561"  .08759124 "00-2022"
    "521"  .02919708 "00-2022"
    "22"   .05839416 "00-2022"
    "1048" .05109489 "00-2022"
    "75"    .0729927 "00-2022"
    "80"   .02919708 "00-2022"
    "194"  .13868614 "00-2022"
    "250"  .08029197 "00-2022"
    "174"  .13138686 "00-2022"
    "123"  .04379562 "00-2022"
    "53"   .05839416 "00-2022"
    "39"   .15328467 "00-2022"
    "55"   .10948905 "00-2022"
    "746"  .02919708 "00-2022"
    "199"  .05109489 "00-2022"
    "180"  .02919708 "00-2022"
    "288"  .02919708 "00-2022"
    "13"   .05839416 "00-2022"
    "95"   .05109489 "00-2022"
    "878"  .02919708 "00-2022"
    "204"  .05109489 "00-2022"
    "27"   .02919708 "00-2022"
    "57"   .13868614 "00-2022"
    "284"  .05839416 "00-2022"
    "171"  .10218978 "00-2022"
    "47"    .0729927 "00-2022"
    "107"  .03649635 "00-2022"
    "688"  .05109489 "00-2022"
    "52"   .05109489 "00-2022"
    "676"  .04379562 "00-2022"
    "475"  .02919708 "00-2022"
    "152"  .02919708 "00-2022"
    "713"  .02919708 "00-2022"
    "32"   .08759124 "00-2022"
    "59"   .05839416 "00-2022"
    "335"  .09489051 "00-2022"
    "3"     .1678832 "00-2022"
    "17"   .11678832 "00-2022"
    "2158" .02919708 "00-2022"
    "44"   .05109489 "00-2022"
    "1314" .05839416 "00-2022"
    "1067" .02919708 "00-2022"
    "215"  .02919708 "00-2022"
    "1002" .02919708 "00-2022"
    "287"  .02919708 "00-2022"
    "917"  .02919708 "00-2022"
    "50"   .02919708 "00-2022"
    "677"  .02919708 "00-2022"
    "156"  .08759124 "00-2022"
    "547"  .02919708 "00-2022"
    "549"  .02919708 "00-2022"
    "103"   .0729927 "00-2022"
    "203"  .02919708 "00-2022"
    "324"  .02919708 "00-2022"
    "662"  .03649635 "00-2022"
    "623"  .02919708 "00-2022"
    "175"  .02919708 "00-2022"
    "360"  .02919708 "00-2022"
    "271"  .02919708 "00-2022"
    "76"   .02919708 "00-2022"
    "139"  .02919708 "00-2022"
    "26"   .04379562 "00-2022"
    "74"   .02919708 "00-2022"
    "310"  .02919708 "00-2022"
    "412"  .02919708 "00-2022"
    "77"   .05109489 "00-2022"
    "69"   .08759124 "00-2022"
    "9"    .09489051 "00-2022"
    "323"  .02919708 "00-2022"
    "38"   .06569343 "00-2022"
    "182"  .02189781 "00-2022"
    "60"   .02189781 "00-2022"
    "668"  .02189781 "00-2022"
    "24"   .03649635 "00-2022"
    "375"  .04379562 "00-2022"
    "1658" .02919708 "00-2022"
    "158"  .02919708 "00-2022"
    "197"  .04379562 "00-2022"
    "224"  .02919708 "00-2022"
    "222"  .02919708 "00-2022"
    "683"  .02919708 "00-2022"
    "30"   .06569343 "00-2022"
    "358"  .02919708 "00-2022"
    "291"  .02919708 "00-2022"
    "686"  .02919708 "00-2022"
    "90"   .02919708 "00-2022"
    end

  • #2
    I tried

    gen wanted = weekly(node_week_year1, "WY")
    format wanted %tw

    But this does not deal with 00-2022 and 53-2023

    Comment


    • #3
      Several datetime functions in this area, including:

      Code:
      help yw()
      help weekly()
      Code:
      gen wanted= yw(real(substr(node, -4, 4)), real(substr(node, 1, 2))+1)
      format wanted %tw
      Res.:

      Code:
      . l in 1/10, sep(0)
      
           +-------------------------------------+
           |   id   degree~y   node_w~1   wanted |
           |-------------------------------------|
        1. |  505   .0291971    00-2022   2022w1 |
        2. |  978   .0291971    00-2022   2022w1 |
        3. |  394   .0510949    00-2022   2022w1 |
        4. |  630   .0291971    00-2022   2022w1 |
        5. |   93    .080292    00-2022   2022w1 |
        6. |   84   .0291971    00-2022   2022w1 |
        7. |   20   .2335766    00-2022   2022w1 |
        8. |  277   .0291971    00-2022   2022w1 |
        9. | 2400   .0291971    00-2022   2022w1 |
       10. | 1559   .0291971    00-2022   2022w1 |
           +-------------------------------------+

      Comment


      • #4
        There isn't a one-to-one correspondence between Stata weeks and any different system.

        As documented, Stata week 1 always starts on 1 January, week 2 always starts on 8 January, and so on, until week 52 which consists of 8 or 9 days depending on whether the year is leap.

        Thus there is never a Stata week 0 or a Stata week 53 in any year.

        You're best advised to avoid Stata weeks completely and to map to the daily dates that start (or end, if more convenient) each week. Then within Stata if you need to tsset or xtset, just specify delta(7).

        More discussion at

        Code:
        . search week, sj
        
        Search of official help files, FAQs, Examples, and Stata Journals
        
        SJ-22-2 dm0107_1  . . .  Erratum: Stata tip 145: Numbering weeks within months
                . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                Q2/22   SJ 22(2):465--466                                (no commands)
                errata for tip on numbering weeks within months
        
        SJ-22-1 dm0107  . . . . . . . . . Stata tip 145: Numbering weeks within months
                . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                Q1/22   SJ 22(1):224--230                                (no commands)
                tip on numbering weeks within months
        
        SJ-19-3 dm0100  . . . . . . . . . .  Speaking Stata: The last day of the month
                . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                Q3/19   SJ 19(3):719--728                                (no commands)
                discusses three related problems about getting the last day
                of the month in a new variable
        
        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
        Other way round, I don't know the definition of week in Python to advise on detail.

        Comment


        • #5
          My analysis is at the week level and all of the data I built on python had to be in week-year format.

          I tried Andrew's suggestion and it worked well except weeks 52 and 53 did not get converted

          Comment


          • #6
            As Nick noted in #4, you need to provide your definition of weeks.

            Originally posted by Nick Cox View Post
            As documented, Stata week 1 always starts on 1 January, week 2 always starts on 8 January, and so on, until week 52 which consists of 8 or 9 days depending on whether the year is leap.
            Do you have a 1:1 lagged correspondence with Stata weeks until week 51 i.e., 00-yyyy = 01-yyyy,..., 50-yyyy= 51-yyyy, but 51-yyyy \(\neq\) 52-yyyy?

            Comment

            Working...
            X