Announcement

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

  • string monthly date into Stata monthly date

    Hi everyone,

    I'm having a problem converting a string monthly date into Stata monthly date. The string date starts in 1999 and goes until 2013 and is shown as:
    Jan-99
    Feb-11
    and so on.

    I've tried using date(var,"MY") to no avail. The problem seem not to have the day in the originalvariable or that months are in text and not integers. I really don't need (because i don't have) the day, but need to have the data in a monthly format that Stata can interpret as such. Any ideas?

    best wishes,

    Nuno

  • #2
    date() is a function for producing daily dates; it is not a generic function for producing any kind of date. This is multiply documented e.g. http://www.stata.com/help.cgi?date() but my own view is that date() should go undocumented in favour of daily()

    Hence you need a different function or command. Experiment with monthly() to see that it is one solution:


    Code:
    . di monthly("Jan-11", "MY", 2050)
    612
    
    . di monthly("Jan-99", "MY", 2050)
    468
    With that solution you then need to specify a display format.

    Alternatively, numdate (SSC) is a one-stop command for producing date variables.

    Code:
    . set obs 2
    obs was 0, now 2
    
    . gen sdate = cond(_n == 1, "Jan-99", "Jan-11")
    
    . numdate monthly mdate = sdate, pattern(MY) topyear(2050)
    
    . l
    
         +-----------------+
         |  sdate    mdate |
         |-----------------|
      1. | Jan-99   1999m1 |
      2. | Jan-11   2011m1 |
         +-----------------+
    Note that date() or daily() is not useless here as you can follow with mofd():

    Code:
     
    . di mofd(daily("Jan-99", "MY", 2050))
    468
    Dates are a large and complicated topic unless you have just recent calendar years and everything is easy. There is no easy substitute for skimming http://www.stata.com/help.cgi?dates

    Last edited by Nick Cox; 20 Oct 2015, 07:39.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      date() is a function for producing daily dates; it is not a generic function for producing any kind of date. This is multiply documented e.g. http://www.stata.com/help.cgi?date() but my own view is that date() should go undocumented in favour of daily()

      Hence you need a different function or command. Experiment with monthly() to see that it is one solution:


      Code:
      . di monthly("Jan-11", "MY", 2050)
      612
      
      . di monthly("Jan-99", "MY", 2050)
      468
      With that solution you then need to specify a display format.

      Alternatively, numdate (SSC) is a one-stop command for producing date variables.

      Code:
      . set obs 2
      obs was 0, now 2
      
      . gen sdate = cond(_n == 1, "Jan-99", "Jan-11")
      
      . numdate monthly mdate = sdate, pattern(MY) topyear(2050)
      
      . l
      
      +-----------------+
      | sdate mdate |
      |-----------------|
      1. | Jan-99 1999m1 |
      2. | Jan-11 2011m1 |
      +-----------------+
      Note that date() or daily() is not useless here as you can follow with mofd():

      Code:
      . di mofd(daily("Jan-99", "MY", 2050))
      468
      Dates are a large and complicated topic unless you have just recent calendar years and everything is easy. There is no easy substitute for skimming http://www.stata.com/help.cgi?dates
      thanks Nick! the monthly command worked perfectly!

      Comment


      • #4
        Good, but monthly() is a function, not a command!

        Comment

        Working...
        X