Announcement

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

  • Dates: conversion and formatting

    Dear Statalists, I am using monthly data that are like "01jan1980" "01feb1980" etc. After the uploading, I use the appropriate conversion function before selecting the format:
    Code:
    gen mdate = mofd(month)
    format mdate %tm
    and I get the requested values "1980m1", "1980m2", etc. However, when I draw a line plot like the following:

    Code:
    line state mdate if mdate>1985m1, tline(1991m1 2009m1)
    The tline(...) works as a charm, but the "if mdate>1985m1" it doesn't. Note that if I use "if mdate>1985" instead, it drops all the variables. What I am doing wrong?

    Thanks a lot,
    Ale

  • #2
    You need to enclose the 1985m1 into some (pseudo) function like tm(1985m1). In other words, you need to find the way to tell Stata that 1985m1 is a tm date.

    If you show a sample of your data using -dataex- it will be easier. Date functions are powerful and complicated, and a lot of what is done with them is trial and error.

    Comment


    • #3
      Thanks Joro, here you have it:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input int month double(ip cpi ff ci_loans loans) float(mdate state)
      3288 39.3405 35.7  6.300645161290323        .                  . 108         .
      3319 39.5922 35.8  6.612857142857143        .                  . 109         .
      3347 39.9027 36.1  6.793225806451613        .                  . 110         .
      3378 39.7559 36.3  7.414666666666666        .                  . 111         .
      3408 39.6056 36.4  8.670322580645161        .                  . 112         .
      3439 39.9925 36.6  8.901333333333334        .                  . 113         .
      3469 40.2037 36.8  8.613225806451613        .                  . 114         .
      3500 40.2961 36.9  9.185483870967742        .                  . 115         .
      3531  40.287 37.1               9.15        .                  . 116         .
      3561 40.2988 37.3                  9        .                  . 117         .
      3592 39.9189 37.5               8.85        .                  . 118         .
      3622 39.8118 37.7  8.971935483870968        .                  . 119         .
      3653 39.0746 37.9  8.976774193548387        .                  . 120         .
      3684 39.0488 38.1  8.978214285714285        .                  . 121         .
      3712 38.9981 38.3  7.762258064516129        .                  . 122         .
      3743 38.8979 38.5  8.096333333333334        .                  . 123         .
      3773 38.8525 38.6  7.946451612903226        .                  . 124         .
      3804 38.7269 38.8              7.605        .                  . 125         .
      3834 38.8219 38.9  7.214838709677419        .                  . 126         .
      3865 38.7527   39  6.615161290322581        .                  . 127         .
      3896 38.4856 39.2  6.289666666666666        .                  . 128         .
      3926 37.7157 39.4  6.203870967741936        .                  . 129         .
      3957 37.4873 39.6  5.601333333333334        .                  . 130         .
      3987 38.3482 39.8  4.900645161290322        .                  . 131         .
      4018 38.6432 39.9  4.142258064516129        .                  . 132         .
      4049 38.5695 39.9 3.7207142857142856        .                  . 133         .
      4077 38.5278   40 3.7116129032258063        .                  . 134         .
      4108 38.7441 40.1              4.156        .                  . 135         .
      4138 38.9407 40.3  4.632258064516129        .                  . 136         .
      4169  39.104 40.5  4.911333333333333        .                  . 137         .
      4199 38.9905 40.6  5.308709677419355        .                  . 138         .
      4230 38.7642 40.7  5.566774193548387        .                  . 139         .
      4261 39.3942 40.8 5.5536666666666665        .                  . 140         .
      4291 39.6891 40.9  5.199354838709677        .                  . 141         .
      4322 39.8576   41  4.910333333333333        .                  . 142         .
      4352 40.3177 41.1  4.138387096774194        .                  . 143         .
      4383 41.2876 41.2 3.5058064516129033        .                  . 144         .
      4414 41.6865 41.4   3.29551724137931        .                  . 145         .
      4443 41.9746 41.4  3.829354838709677        .                  . 146         .
      4474 42.4183 41.5              4.171        .                  . 147         .
      4504 42.3963 41.6  4.269677419354839        .                  . 148         .
      4535 42.5166 41.7  4.464666666666667        .                  . 149         .
      4565  42.498 41.8  4.549032258064516        .                  . 150         .
      4596 43.0629 41.9   4.80516129032258        .                  . 151         .
      4627 43.3918 42.1  4.874333333333333        .                  . 152         .
      4657 43.9718 42.2   5.04516129032258        .                  . 153         .
      4688 44.4919 42.4  5.056333333333333        .                  . 154         .
      4718 45.0015 42.5  5.332258064516129        .                  . 155         .
      4749 45.2942 42.7  5.944516129032258 135413.5             405165 156  .3342181
      4780 45.9656   43 6.5842857142857145 140798.3         415619.575 157  .3387673
      4808 45.9872 43.4  7.090322580645162 144957.6 425590.39999999997 158  .3406036
      4839 45.9237 43.7  7.124333333333333 147534.3          430881.05 159 .34240144
      4869 46.2192 43.9  7.839354838709677 149965.9 438160.66000000003 160  .3422624
      4900 46.2509 44.2              8.491 152829.2           445636.7 161  .3429457
      4930 46.4421 44.2               10.4 155329.1 451669.69999999995 162  .3438998
      4961 46.3648   45 10.495806451612903 158068.1          458236.06 163  .3449491
      4992 46.7803 45.2 10.784666666666666 158506.6         461724.375 164  .3432927
      5022 47.0954 45.6              10.01 159169.4 464365.27999999997 165  .3427677
      5053 47.3445 45.9 10.033666666666667 159935.3         467151.275 166   .342363
      5083 47.2315 46.3   9.94774193548387 161211.3         473343.975 167  .3405796
      5114 46.8994 46.8  9.648387096774194 163022.6          476272.26 168  .3422887
      5145 46.7538 47.3  8.971785714285714   164772         479701.625 169  .3434885
      5173 46.7685 47.8  9.346129032258064 168816.8           486869.8 170  .3467391
      5204 46.6069 48.1             10.513 174990.9           498445.7 171 .35107315
      5234 46.9743 48.6 11.306129032258065 177630.9 506040.72000000003 172   .351021
      5265 46.9233   49             11.933 180108.2           512532.2 173  .3514085
      5295 46.9464 49.3 12.922903225806452 183987.3          519824.98 174  .3539408
      5326 46.4907 49.9 12.005161290322581 186381.9          523445.95 175  .3560671
      5357 46.5136 50.6 11.343333333333334 187994.1         526027.675 176  .3573844
      5387 46.3401   51 10.057741935483872 189230.2          526668.62 177  .3592965
      5418 44.8209 51.5  9.447666666666667 190404.7  527347.6749999999 178   .361061
      5448  43.234 51.9  8.530967741935484 191276.1  530698.2000000001 179  .3604235
      5479 42.6409 52.3  7.131612903225807 191586.9          529030.38 180  .3621472
      5510 41.6602 52.6  6.240714285714286 190380.5  524375.9750000001 181  .3630611
      5538 41.2135 52.8  5.537419354838709 188119.1          523750.45 182   .359177
      5569  41.246   53  5.492666666666667 187410.9 521681.83999999997 183  .3592437
      5599 41.1515 53.1 5.2212903225806455   186074           521016.8 184  .3571363
      5630 41.4224 53.5              5.548   184638 521232.94999999995 185  .3542332
      5660 41.8234   54  6.095806451612903   183651 520414.63999999996 186  .3528936
      5691 42.2573 54.2  6.135483870967742 182928.2           519864.9 187  .3518764
      5722 42.7796 54.6              6.239   182822 520629.72500000003 188  .3511555
      5752 42.9687 54.9  5.816129032258065 182714.2          522525.88 189  .3496749
      5783 43.0614 55.3 5.2156666666666665 182618.9 521804.60000000003 190 .34997565
      5813 43.5973 55.6  5.195483870967742 183358.6  528126.7000000001 191  .3471868
      5844 44.2288 55.8  4.867741935483871 182648.1         524858.925 192  .3479947
      5875 44.6725 55.9  4.770689655172414 182044.4          525653.65 193  .3463201
      5904 44.7125   56   4.83741935483871 179487.7  529345.0599999999 194  .3390751
      5935 44.9643 56.1              4.816 177475.5           530415.8 195  .3345969
      5965 45.1741 56.4  5.290645161290323 178432.6         533934.975 196  .3341841
      5996  45.183 56.7  5.476333333333334   179102  540356.3799999999 197  .3314516
      6026 45.4535   57  5.306451612903226 179142.3  542022.2250000001 198  .3305073
      6057 45.7737 57.3  5.290967741935484 179794.9  544463.2000000001 199  .3302242
      6088 45.9031 57.6              5.252 181173.2           548417.7 200  .3303562
      6118 45.9207 57.9  5.024838709677419 182438.3  554317.5750000001 201  .3291223
      6149 46.5978 58.1              4.949 183900.5           558069.3 202 .32952985
      6179 47.0854 58.4  4.651290322580645 185247.8  565706.4400000001 203  .3274628
      6210 46.8276 58.7  4.610967741935484 185787.4          566563.45 204  .3279198
      6241 47.5417 59.3 4.6832142857142856 187411.3         569658.725 205  .3289887
      6269  48.131 59.6               4.69 189154.7          576559.42 206  .3280749
      6300 48.5838   60  4.727333333333333 190294.8  583249.5750000001 207  .3262665
      end
      format %tm month
      format %tm mdate

      Comment


      • #4
        So this does not work
        Code:
        . summ cpi if mdate>1970m1
        1970m1 invalid name
        r(198);
        because Stata does not know that we are supplying a date.

        However this works because we told Stata that it is a date:

        Code:
        . summ cpi if mdate>tm(1970m1)
        
            Variable |        Obs        Mean    Std. Dev.       Min        Max
        -------------+---------------------------------------------------------
                 cpi |         87    47.21034    6.925384       38.1         60

        Comment


        • #5
          Thanks for the data example. You have a monthly dataset but one variable is a daily date, not a monthly date. Therefore you need a daily date format for that.

          This should suffice to show the problem with assigning a monthly date display format to a daily date.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input int month float mdate
          3288 108
          3319 109
          3347 110
          3378 111
          3408 112
          3439 113
          3469 114
          3500 115
          3531 116
          3561 117
          end
          format %tm month
          format %tm mdate
          . l
          
               +-------------------+
               |   month     mdate |
               |-------------------|
            1. |  2234m1    1969m1 |
            2. |  2236m8    1969m2 |
            3. | 2238m12    1969m3 |
            4. |  2241m7    1969m4 |
            5. |  2244m1    1969m5 |
               |-------------------|
            6. |  2246m8    1969m6 |
            7. |  2249m2    1969m7 |
            8. |  2251m9    1969m8 |
            9. |  2254m4    1969m9 |
           10. | 2256m10   1969m10 |
               +-------------------+
          
          . format month %td
          
          . list
          
               +---------------------+
               |     month     mdate |
               |---------------------|
            1. | 01jan1969    1969m1 |
            2. | 01feb1969    1969m2 |
            3. | 01mar1969    1969m3 |
            4. | 01apr1969    1969m4 |
            5. | 01may1969    1969m5 |
               |---------------------|
            6. | 01jun1969    1969m6 |
            7. | 01jul1969    1969m7 |
            8. | 01aug1969    1969m8 |
            9. | 01sep1969    1969m9 |
           10. | 01oct1969   1969m10 |
               +---------------------+

          Changing the display format is not a way to convert one kind of date to another.

          In terms of your specific question

          Code:
          if mdate >  tm(1985m1)
          
          if mdate > ym(1985. 1)
          are two solutions, the first as Joro Kolev flagged and the second just another way to do it.

          Comment


          • #6
            Great, thanks both for the help!

            Comment


            • #7
              The numdate package at SSC is a way of bundling up Stata's official date functions for perhaps most of the problems that arise in practice.

              Necessarily, however, it cannot do everything that you might want to do with dates, and the individual functions and display formats remain indispensable.
              Last edited by Nick Cox; 12 Apr 2021, 05:23.

              Comment

              Working...
              X