Announcement

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

  • Help Extracting Year from Date

    Hello everyone, I'm brand new to learning STATA, so I apologize if I am omitting any pertinent information. I have looked through previous posts on this topic, but am having trouble understanding how to code it.

    I am attempting to extract the year from the date listed.

    The data is currently listed in this format: testdate is 2012-03-12 00:00:00

    I would like to simply create a new column with the year from the testdate column.
    I really appreciate the help.
    Thanks.
    Lori

  • #2
    Code:
    gen year = year(dofc(testdate))
    may help.

    Comment


    • #3
      Hi Nick, thanks for your help. I'm getting a type mismatch error. I already tried destringing the date.

      gen testyear = year(dofc(testdate))

      type mismatch
      r(109);

      Thanks again.
      L

      Comment


      • #4
        -destring- should never be used with string variables that look like dates. It is only for string variables that look like real numbers, and where the numeric value they look like is actually what you need to work with.

        Code:
        gen testyear = year(dofc(clock(testdate, "YMDhms")))
        It is precisely because of things like the impossibility of knowing from a description as in #1 whether a variable is a string representation or an actual Stata internal format date variable that the Forum FAQ (which all members are asked to read before posting) advises using the -dataex- command to post example data. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.



        When asking for help with code, always show example data. When showing example data, always use -dataex-.

        Comment


        • #5
          Thanks again. I appreciate your help, Clyde. I apologize for the lack of knowledge in posting.
          -L

          Comment


          • #6
            If testdate is a string then


            Code:
            gen year = real(substr(testdate, 1, 4))
            should be sufficient.

            Comment


            • #7
              Nick is correct in #6, provided that the formatting of testdate is uniform. But if some of the values have leading blanks or other typographical variants, this code might not work. The nice thing about the -clock()- function is that it is somewhat forgiving in this respect.

              Comment

              Working...
              X