Announcement

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

  • translating month year strings into monthly stata time

    hello we have month year strings and used the date(S, "MY") command. The resulting months are completely off. can anyone tell us what the problem is?
    thanks. the data is below
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str7 DDN float ddn
    "05/1975"    5599
    "11/1984"    9071
    "12/1984"    9101
    "12/1970"    3987
    "11/1975"    5783
    "09/1974"    5357
    "09/1954"   -1948
    "06/1976"    5996
    "05/0719" -453146
    "01/1975"    5479
    "04/1968"    3013
    "09/1978"    6818
    "11/1951"   -2983
    "10/1962"    1004
    "10/1994"   12692
    "12/1980"    7640
    "12/1997"   13849
    "12/1979"    7274
    "04/1972"    4474
    "03/1987"    9921
    "05/1986"    9617
    "06/1955"   -1675
    "08/1988"   10440
    "07/1995"   12965
    "07/1995"   12965
    "09/1976"    6088
    "10/1970"    3926
    "12/1984"    9101
    "05/1963"    1216
    "04/1979"    7030
    "05/1973"    4869
    "02/1981"    7702
    "11/1948"   -4078
    "06/1991"   11474
    "05/1991"   11443
    "12/1972"    4718
    "11/1960"     305
    "12/1972"    4718
    "11/1986"    9801
    "07/1988"   10409
    "10/1977"    6483
    "01/1988"   10227
    "10/2004"   16345
    "12/1990"   11292
    "02/1977"    6241
    "09/1966"    2435
    "09/1979"    7183
    "01/1981"    7671
    "05/1979"    7060
    "08/1975"    5691
    "06/1959"    -214
    "07/1975"    5660
    "03/1995"   12843
    "10/1958"    -457
    "08/2000"   14823
    "01/1967"    2557
    "09/1982"    8279
    "12/1996"   13484
    "01/1983"    8401
    "02/1976"    5875
    "04/1971"    4108
    "12/1996"   13484
    "12/1998"   14214
    "10/1970"    3926
    "12/1985"    9466
    "07/1975"    5660
    "07/1993"   12235
    "03/1959"    -306
    "01/1961"     366
    "09/1978"    6818
    "01/1963"    1096
    "12/1970"    3987
    "07/1989"   10774
    "03/1960"      60
    "08/1979"    7152
    "05/1981"    7791
    "10/1960"     274
    "03/1976"    5904
    "08/1964"    1674
    "07/1988"   10409
    "08/1990"   11170
    "06/1992"   11840
    "02/1983"    8432
    "08/1960"     213
    "03/1979"    6999
    "11/1991"   11627
    "10/1984"    9040
    "11/1972"    4688
    "04/1980"    7396
    "08/1948"   -4170
    "09/1988"   10471
    "05/1991"   11443
    "03/1982"    8095
    "11/1979"    7244
    "11/1967"    2861
    "06/1957"    -944
    "12/1987"   10196
    "11/1981"    7975
    "04/1965"    1917
    "07/1984"    8948
    end
    format %tmMonth_CCYY ddn

  • #2
    date() is for producing daily dates, as is documented.

    date(s1,s2[,Y])
    Description: the e_d date (days since 01jan1960) corresponding to
    s1 based on s2 and Y

    s1 contains the date, recorded as a string, in
    virtually any format. Months can be spelled out,
    abbreviated (to three characters), or indicated as
    numbers; years can include or exclude the century;
    blanks and punctuation are allowed.

    s2 is any permutation of M, D, and [##]Y, with their
    order defining the order that month, day, and year
    occur in s1. ##, if specified, indicates the
    default century for two-digit years in s1. For
    instance, s2="MD19Y" would translate s1="11/15/91"
    as 15nov1991.

    Y provides an alternate way of handling two-digit
    years. When a two-digit year is encountered, the
    largest year, topyear, that does not exceed Y is
    returned.

    date("1/15/08","MDY",1999) = 15jan1908
    date("1/15/08","MDY",2019) = 15jan2008

    date("1/15/51","MDY",2000) = 15jan1951
    date("1/15/50","MDY",2000) = 15jan1950
    date("1/15/49","MDY",2000) = 15jan1949

    date("1/15/01","MDY",2050) = 15jan2001
    date("1/15/00","MDY",2050) = 15jan2000

    If neither ## nor Y is specified, date() returns
    missing when it encounters a two-digit year. See
    Working with two-digit years in [D] Datetime
    conversion for more information.
    Domain s1: strings
    Domain s2: strings
    Domain Y: integers 1000 to 9998 (but probably 2001 to 2099)
    Range: e_d dates 01jan0100 to 31dec9999 (integers -679,350
    to 2,936,549) or missing

    daily(s1,s2[,Y])
    Description: a synonym for date(s1,s2[,Y])


    This is a little clearer when you consider that daily() is the same code under a different name. You need monthly().

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str7 DDN float ddn
    "05/1975"    5599
    "11/1984"    9071
    "12/1984"    9101
    "12/1970"    3987
    "11/1975"    5783
    "09/1974"    5357
    "09/1954"   -1948
    end
    
    gen wanted = monthly(DDN, "MY")
    format wanted %tm
    format ddn %td
    
    list, sep(0)
    
        +-------------------------------+
         |     DDN         ddn    wanted |
         |-------------------------------|
      1. | 05/1975   01may1975    1975m5 |
      2. | 11/1984   01nov1984   1984m11 |
      3. | 12/1984   01dec1984   1984m12 |
      4. | 12/1970   01dec1970   1970m12 |
      5. | 11/1975   01nov1975   1975m11 |
      6. | 09/1974   01sep1974    1974m9 |
      7. | 09/1954   01sep1954    1954m9 |
         +-------------------------------+

    Comment


    • #3
      Thanks a lot!

      Comment

      Working...
      X