Announcement

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

  • Removing slashes for a date variable

    Hello, I have a date variable that looks like this: 2007/06/20. How can I remove the slashes so that I create three new variables: year, month, and date? Also, how can I get rid of the slashes so that I replace it to look like 20070620?

  • #2
    Something like this would work:
    Code:
    clear all
    input str10 datevar
    "2007/06/20"
    "2007/06/21"
    "2007/06/22"
    end
    
    gen year = substr(datevar, 1, 4)
    gen temp = substr(datevar, 6, 10)
    gen month = substr(temp, 1, 2)
    gen day = substr(datevar, 9, 10)
    gen new_date = year + month + day
    drop temp

    Comment


    • #3
      To add, you might also want create a date format that Stata understands (for use as a time variable in tsset or xtset):

      Code:
      gen datenew=date(date,"YMD")
      format  datenew %td
      The second line is optional, and is just the display format; for more display format options see http://www.stata.com/manuals13/ddate...layformats.pdf

      Comment


      • #4
        As Jorrit suggest it the date() function will give you a date (which is a number) from the date variable which type is string and can deal with the slashes. Then you can use related date functions (year(), month(), etc.) instead of using string functions. See help date() and the related documentation on dates.

        Code:
        di %td date("2006/27/10","YMD")
        . 20jun2010
        Code:
        clear all
        input str10 datevar
        "2007/06/20"
        "2007/06/21"
        "2007/06/22"
        end
        
        
        gen date = date(datevar,"YMD")
        format date %td
        year = year(date)
        month = month(date)
        
        
        list

        Comment


        • #5
          As I've often written, I recommend using daily() rather than date() here. It's the same function, so what difference does that make? Although Stata documentation never encourages this, people often imagine that date() is a generic function for producing all kinds of dates, so that presumably it is supposed smart enough to work out what kind of date you have.

          The more people say daily(), the less likely that will be. (Naturally StataCorp would help most by letting date() go undocumented.)

          Comment


          • #6
            Nick Cox
            Thanks for the advice. I shall apply it next time and recommend it as well when asked how to deal with dates in Stata.

            Comment


            • #7
              Christophe: Thanks for your comment. Note that your code in #4 needs two insertions of generate (I imagine that you were thinking in Mata).

              Comment


              • #8
                Yes you're right, I forgot the generates. Thanks.
                I'm more thinking in SAS at the moment :-)

                Comment

                Working...
                X