Announcement

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

  • How to convert weekly to yearly

    I have a question about this:

    I have weekly data, but that is described in number of weeks, i.e., (1,2,3,....,400). The first week is the week of 1990/09/01, and so on so forth. My goal is to convert these weeks into year, for example, a new variable "year" likes (1990, 1991,....)

    Thanks so much for your help!

  • #2
    Well, seems actually easier to go straight to year than to convert to dates.

    Code:
    clear
    set obs 400
    gen theweek=_n
    gen year=1990+floor((theweek-1)/52)

    Comment


    • #3
      Originally posted by ben earnhart View Post
      Well, seems actually easier to go straight to year than to convert to dates.

      Code:
      clear
      set obs 400
      gen theweek=_n
      gen year=1990+floor((theweek-1)/52)
      Thank you Ben

      Another problem is my data is panel and for each week I also have many variables (e.g., many stores sales information), so it is not easy to simply gen obs.

      Could you please help on converting dates?

      Comment


      • #4
        please paste a chunk of your data (and read the FAQs on how to do so). I assumed you were merely concerned with the single variable that was the week #. In my code, you could ignore the first three lines -- I was just generating observations 1-400 to try to simulate your data. Is the data already in date format, and is it long or wide?

        Comment


        • #5
          This might be of help http://www.opendoors.pk/STATA/cumula...o-monthly-data
          Regards
          --------------------------------------------------
          Attaullah Shah, PhD.
          Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
          FinTechProfessor.com
          https://asdocx.com
          Check out my asdoc program, which sends outputs to MS Word.
          For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

          Comment


          • #6
            Weeks outside Stata don't correspond to weeks inside Stata.

            So, avoid week functions.

            If you convert to daily dates

            Code:
            gen day = mdy(9,1,1990) + (week - 1) * 7
            then you can extract the year using year():

            Code:
            gen year = year(day)
            See (e.g.) http://www.stata-journal.com/article...article=dm0052 and later notes by the same author on weeks in the Stata Journal.

            (Having panel data or other variables seems of no consequence here.)
            Last edited by Nick Cox; 25 Dec 2014, 04:09.

            Comment


            • #7
              Originally posted by Nick Cox View Post
              Weeks outside Stata don't correspond to weeks inside Stata.

              So, avoid week functions.

              If you convert to daily dates

              Code:
              gen day = mdy(9,1,1990) + (week - 1) * 7
              then you can extract the year using year():

              Code:
              gen year = year(day)
              See (e.g.) http://www.stata-journal.com/article...article=dm0052 and later notes by the same author on weeks in the Stata Journal.

              (Having panel data or other variables seems of no consequence here.)
              Thank you Nick, That works.

              Regards.

              Comment

              Working...
              X