Announcement

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

  • How to properly use %tm formatted dates imported from .csv files?

    I have the attached .csv file with date format in %tm format, which looks like:
    Code:
        MONTH,Y,X
        2000m1,0.0979083,-0.1030651
        2000m10,-0.0543795,0.1151973
        2000m11,-0.0050536,0.1228735
        2000m12,0.0302205,0.0029895
    When I try to run a regression on a specific duration of the sample I get the error:
    Code:
    regress y x if month>=tm(2001m1) & month<=tm(2006m9)
    type mismatch
    r(109);
    The manual seem a little bit cryptic.
    I think this has something to do with the variable being imported in a string format, since in the properties of the variables I see:
    Code:
    Name: month
    Label: MONTH
    Type: str7
    Format:%9s
    I tried to generate a new variable with:
    Code:
    gen YEARMONTH = date(MONTH, "YM")
    but with no success.

    How do I run the conditional regression I want?

    Reproducible example:
    Code:
    insheet using stata-stack.csv
    save stata-stack.dta
    regress y x if month>=tm(2001m1) & month<=tm(2006m9)
    Attached Files

  • #2
    The -date()- function requires a fully specified date, not just month and year. You need the -monthly()- function:

    Code:
    gen yearmonth = monthly(month, "YM")

    Comment


    • #3
      Further to Clyde's excellent advice, note that the word format is heavily overloaded.

      %tm is to Stata a display format such that, for example, a numeric date like 480 displays as 2000m1.

      Code:
      . di %tm 480
       2000m1
      A monthly date, indeed any date, is stored as numeric in Stata, the origin for any set of dates being the start of 1960. (Calendar years are the only exception.) That origin makes most dates unintelligible to humans, which is why display formats exist for you to exploit.

      It's not surprising that you want to regard strings such as "2000m1" as having %tm format, but that's at best indirect. Stata doesn't have a term storage format; that's at most in the eye of the beholder, or part of your private language.

      Conversely, Stata can work with dates presented as strings:

      Code:
       
      . di monthly("2000m1", "YM")
      480
      
      . di %tm monthly("2000m1", "YM")
       2000m1
      but that doesn't depend on their being in, or more precisely resembling, a numeric display format.


      Comment

      Working...
      X