Announcement

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

  • Date Help: (DM) Dates without Years and Unequal Intervals

    Sorry this is a very basic problem. I have a time series covering 1.5 years. The datevar is a string without the corresponding years. And worse the date changes are unequal. Such as:
    Code:
    03-12 
    
    05-12
    To corresponding to “DM” or December 3rd and then the 5th. The last entry is 2019 so I know the first date is: December 3rd, 2018. Ideally I was looking to convert it to standard %td format (12/03/18). But can’t manage to find anything in the Date manuals. The dates don’t change with equal interval, so I can’t create a separate variable without referencing this one.

    Apologies, this seems like a simple command fix. Appreciate the help.

  • #2
    Hey, could you send a bit more information, i.e. code and output of what you are trying to attempt?

    Marc

    Comment


    • #3
      Originally posted by Marc Torra View Post
      Hey, could you send a bit more information, i.e. code and output of what you are trying to attempt?

      Marc
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str5(Date Oppt Minutes) float FDP str7 FDPrice float DKP str7 DKPrice float SFP str3 SFPrice
      "03-06" ""      ""         . ""            . ""           . ""  
      "03-05" "@ Min" "^38.5" 60.6 "$11,400" 66.25 "$11,300" 60.6 "$54"
      "03-04" ""      ""         . ""            . ""           . ""  
      "03-03" "Mem"   "^35.5" 44.7 "$11,600"    45 "$11,600" 44.7 "$55"
      "03-02" "@ SAS" "^33.8" 37.8 "$11,700" 41.25 "$11,500" 37.8 "$55"
      "03-01" ""      ""         . ""            . ""           . ""  
      "02-28" "Phi"   "^37.5" 54.7 "$11,600" 60.25 "$11,100" 54.7 "$55"
      "02-27" ""      ""         . ""            . ""           . ""  
      "02-26" "@ Den" "^35.5" 45.3 "$12,200"    52 "$11,000" 45.3 "$55"
      "02-25" ""      ""         . ""            . ""           . ""  
      "02-24" ""      ""         . ""            . ""           . ""  
      "02-23" "Sac"   "^35.3"   58 "$11,900"    64 "$11,300"   58 "$54"
      "02-22" "Uta"   "^42.9"   77 "$11,800" 81.75 "$11,500"   77 "$48"
      "02-21" ""      ""         . ""            . ""           . ""  
      "02-14" "@ Nor" "^39.0" 81.3 "$12,400"    88 "$11,600" 81.3 "$53"
      "02-13" ""      ""         . ""            . ""           . ""  
      "02-12" ""      ""         . ""            . ""           . ""  
      "02-11" "Por"   "^36.9" 57.3 "$12,100"    63 "$11,400" 57.3 "$53"
      "02-10" ""      ""         . ""            . ""           . ""  
      "02-09" "@ Hou" "^38.4" 50.9 "$12,100"  58.5 "$11,200" 50.9 "$53"
      "02-08" ""      ""         . ""            . ""           . ""  
      "02-07" "Mem"   "^33.2" 64.1 "$12,000" 66.25 "$11,300" 64.1 "$53"
      "02-06" ""      ""         . ""            . ""           . ""  
      "02-05" "Orl"   "^35.8"   57 "$12,000" 63.75 "$11,400"   57 "$53"
      "02-04" ""      ""         . ""            . ""           . ""  
      "02-03" "@ Bos" "^37.1" 62.4 "$11,700"    68 "$11,000" 62.4 "$53"
      "02-02" ""      ""         . ""            . ""           . ""  
      "02-01" "@ Mia" "^31.9" 51.4 "$11,600"  56.5 "$11,000" 51.4 "$5"
      end

      Here's a quick example with Sports data. Actually it's M-D format as seen above but it wouldn't matter in my case without year. But how could I fix the datevar to properly use time series with tsset?
      Last edited by Ali Sheikhpour; 06 Mar 2019, 07:27.

      Comment


      • #4
        Added in edit: this crossed with the sample data in post #3. The dates in post #3 seem to be going from latest to earliest, which means my code posted below will need to be modified to take this into account, along with the reversal of month and day.

        I have generated example data from the 15th day of 18 months ending March 2019, and the code I apply to that example data may point you in a useful direction, especially with regard to assigning years to dates.

        But before you try to understand what this code is doing, Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

        All Stata manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str5 dm
        "15-10"
        "15-11"
        "15-12"
        "15-01"
        "15-02"
        "15-03"
        "15-04"
        "15-05"
        "15-06"
        "15-07"
        "15-08"
        "15-09"
        "15-10"
        "15-11"
        "15-12"
        "15-01"
        "15-02"
        "15-03"
        end
        
        generate d = real(substr(dm,1,2))
        generate m = real(substr(dm,4,2))
        generate y = 2016 + sum(m < m[_n-1])
        
        generate date = mdy(m,d,y)
        format date %tdNN/DD/CCYY
        list, clean
        Code:
        . list, clean
        
                  dm    d    m      y         date  
          1.   15-10   15   10   2017   10/15/2017  
          2.   15-11   15   11   2017   11/15/2017  
          3.   15-12   15   12   2017   12/15/2017  
          4.   15-01   15    1   2018   01/15/2018  
          5.   15-02   15    2   2018   02/15/2018  
          6.   15-03   15    3   2018   03/15/2018  
          7.   15-04   15    4   2018   04/15/2018  
          8.   15-05   15    5   2018   05/15/2018  
          9.   15-06   15    6   2018   06/15/2018  
         10.   15-07   15    7   2018   07/15/2018  
         11.   15-08   15    8   2018   08/15/2018  
         12.   15-09   15    9   2018   09/15/2018  
         13.   15-10   15   10   2018   10/15/2018  
         14.   15-11   15   11   2018   11/15/2018  
         15.   15-12   15   12   2018   12/15/2018  
         16.   15-01   15    1   2019   01/15/2019  
         17.   15-02   15    2   2019   02/15/2019  
         18.   15-03   15    3   2019   03/15/2019
        Last edited by William Lisowski; 06 Mar 2019, 07:42.

        Comment

        Working...
        X