Announcement

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

  • String date

    Hi I am trying to convert the string date into a numeric date so that I can extract the year and month variables.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str10 date float(lon lat value numdate)
    "2016-01-01" 73.9058 33.5133 288.6302 .
    "2016-01-01" 73.9058 33.5133 289.9873 .
    "2016-02-01" 73.9058 33.5133 291.4626 .
    "2016-02-01" 73.9058 33.5133 292.8294 .
    "2016-03-01" 73.9058 33.5133 294.2896 .
    "2016-03-01" 73.9058 33.5133 295.6251 .
    "2016-04-01" 73.9058 33.5133 298.6492 .
    "2016-04-01" 73.9058 33.5133 300.1137 .
    "2016-05-01" 73.9058 33.5133 303.5309 .
    "2016-05-01" 73.9058 33.5133 305.0149 .
    "2016-06-01" 73.9058 33.5133 304.4824 .
    "2016-06-01" 73.9058 33.5133  305.941 .
    "2016-07-01" 73.9058 33.5133 301.8569 .
    "2016-07-01" 73.9058 33.5133 303.2939 .
    "2016-08-01" 73.9058 33.5133 301.7478 .
    "2016-08-01" 73.9058 33.5133 303.1923 .
    "2016-09-01" 73.9058 33.5133 301.0252 .
    "2016-09-01" 73.9058 33.5133 302.4876 .
    "2016-10-01" 73.9058 33.5133 300.2055 .
    "2016-10-01" 73.9058 33.5133 301.6139 .
    "2016-11-01" 73.9058 33.5133 296.2871 .
    "2016-11-01" 73.9058 33.5133 297.6145 .
    "2016-12-01" 73.9058 33.5133  293.532 .
    "2016-12-01" 73.9058 33.5133 294.8982 .
    "2016-01-01" 73.8461 33.1696 286.0074 .
    "2016-01-01" 73.8461 33.1696 287.6523 .
    "2016-02-01" 73.8461 33.1696 289.2243 .
    "2016-02-01" 73.8461 33.1696 291.0409 .
    "2016-03-01" 73.8461 33.1696 292.5378 .
    "2016-03-01" 73.8461 33.1696 294.6417 .
    "2016-04-01" 73.8461 33.1696 297.3839 .
    "2016-04-01" 73.8461 33.1696   300.01 .
    "2016-05-01" 73.8461 33.1696 302.9704 .
    "2016-05-01" 73.8461 33.1696 304.8874 .
    "2016-06-01" 73.8461 33.1696 304.4133 .
    "2016-06-01" 73.8461 33.1696 306.0415 .
    "2016-07-01" 73.8461 33.1696 301.4697 .
    "2016-07-01" 73.8461 33.1696 302.9407 .
    "2016-08-01" 73.8461 33.1696 301.0528 .
    "2016-08-01" 73.8461 33.1696 302.5832 .
    "2016-09-01" 73.8461 33.1696 300.4885 .
    "2016-09-01" 73.8461 33.1696 302.1199 .
    "2016-10-01" 73.8461 33.1696 298.3684 .
    "2016-10-01" 73.8461 33.1696 301.0981 .
    "2016-11-01" 73.8461 33.1696 293.5412 .
    "2016-11-01" 73.8461 33.1696 295.5495 .
    "2016-12-01" 73.8461 33.1696 290.0181 .
    "2016-12-01" 73.8461 33.1696 291.7942 .
    "2016-01-01" 73.8398 33.1855 286.0074 .
    "2016-01-01" 73.8398 33.1855 287.6523 .
    "2016-02-01" 73.8398 33.1855 289.2243 .
    "2016-02-01" 73.8398 33.1855 291.0409 .
    "2016-03-01" 73.8398 33.1855 292.5378 .
    "2016-03-01" 73.8398 33.1855 294.6417 .
    "2016-04-01" 73.8398 33.1855 297.3839 .
    "2016-04-01" 73.8398 33.1855   300.01 .
    "2016-05-01" 73.8398 33.1855 302.9704 .
    "2016-05-01" 73.8398 33.1855 304.8874 .
    "2016-06-01" 73.8398 33.1855 304.4133 .
    "2016-06-01" 73.8398 33.1855 306.0415 .
    "2016-07-01" 73.8398 33.1855 301.4697 .
    "2016-07-01" 73.8398 33.1855 302.9407 .
    "2016-08-01" 73.8398 33.1855 301.0528 .
    "2016-08-01" 73.8398 33.1855 302.5832 .
    "2016-09-01" 73.8398 33.1855 300.4885 .
    "2016-09-01" 73.8398 33.1855 302.1199 .
    "2016-10-01" 73.8398 33.1855 298.3684 .
    "2016-10-01" 73.8398 33.1855 301.0981 .
    "2016-11-01" 73.8398 33.1855 293.5412 .
    "2016-11-01" 73.8398 33.1855 295.5495 .
    "2016-12-01" 73.8398 33.1855 290.0181 .
    "2016-12-01" 73.8398 33.1855 291.7942 .
    "2016-01-01"  73.764 33.1289 286.0074 .
    "2016-01-01"  73.764 33.1289 287.6523 .
    "2016-02-01"  73.764 33.1289 289.2243 .
    "2016-02-01"  73.764 33.1289 291.0409 .
    "2016-03-01"  73.764 33.1289 292.5378 .
    "2016-03-01"  73.764 33.1289 294.6417 .
    "2016-04-01"  73.764 33.1289 297.3839 .
    "2016-04-01"  73.764 33.1289   300.01 .
    "2016-05-01"  73.764 33.1289 302.9704 .
    "2016-05-01"  73.764 33.1289 304.8874 .
    "2016-06-01"  73.764 33.1289 304.4133 .
    "2016-06-01"  73.764 33.1289 306.0415 .
    "2016-07-01"  73.764 33.1289 301.4697 .
    "2016-07-01"  73.764 33.1289 302.9407 .
    "2016-08-01"  73.764 33.1289 301.0528 .
    "2016-08-01"  73.764 33.1289 302.5832 .
    "2016-09-01"  73.764 33.1289 300.4885 .
    "2016-09-01"  73.764 33.1289 302.1199 .
    "2016-10-01"  73.764 33.1289 298.3684 .
    "2016-10-01"  73.764 33.1289 301.0981 .
    "2016-11-01"  73.764 33.1289 293.5412 .
    "2016-11-01"  73.764 33.1289 295.5495 .
    "2016-12-01"  73.764 33.1289 290.0181 .
    "2016-12-01"  73.764 33.1289 291.7942 .
    end

  • #2
    Code:
    gen ndate = date(date, "YMD")
    gen nmon = month(ndate)
    gen nyear = year(ndate)
    If the only goal is to extract year and month, it's also possible to extract that from the string as well without creating and numeric date:

    Code:
    gen nmond2 = real(substr(date, 6, 2))
    gen nyear2 = real(substr(date, 1, 4))

    Comment


    • #3
      Using monthly() to extract monthly dates Is also possible.

      Comment


      • #4
        Elaborating #3 this is evident that these are just monthly data presented for the first day of the month. Month of the year and year itself could be useful, but for other purposes monthly dates are essential.

        At some point it becomes a waste of time to struggle with date-time problems as if they were all impossible. Do read

        Code:
        help datetime
        while skimming and skipping parts that are not needed by you right now.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str10 date
        "2016-01-01"
        "2016-02-01"
        "2016-03-01"
        "2016-04-01"
        "2016-05-01"
        "2016-06-01"
        "2016-07-01"
        "2016-08-01"
        "2016-09-01"
        "2016-10-01"
        "2016-11-01"
        "2016-12-01"
        end
        
        gen mdate = monthly(substr(date, 1, 7), "YM")
        format mdate %tm 
        
        list
        
             +----------------------+
             |       date     mdate |
             |----------------------|
          1. | 2016-01-01    2016m1 |
          2. | 2016-02-01    2016m2 |
          3. | 2016-03-01    2016m3 |
          4. | 2016-04-01    2016m4 |
          5. | 2016-05-01    2016m5 |
             |----------------------|
          6. | 2016-06-01    2016m6 |
          7. | 2016-07-01    2016m7 |
          8. | 2016-08-01    2016m8 |
          9. | 2016-09-01    2016m9 |
         10. | 2016-10-01   2016m10 |
             |----------------------|
         11. | 2016-11-01   2016m11 |
         12. | 2016-12-01   2016m12 |
             +----------------------+

        Comment

        Working...
        X