Announcement

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

  • Truncating numerical dates to years

    I am trying to merge a few datasets, and one of them uses very specific date information in a format that is not, as far as I understand, legible to STATA as a date.

    Specifically, the observations look like this:

    start end
    19460101 19461129
    19460102 19460415
    19460119 19460509
    19460305 19460331


    It is YYYYMMDD, such that 19460101 translates to January 1st, 1946.

    The trouble with this is that the other data sets do not use this specific format, and indeed, I'd rather just use year.

    Is there a way to generate new variables "start_year" and "end_year" such that they only include the first 4 digits of "start" and "end" respectively?

  • #2
    Are those variables numeric or string?

    Comment


    • #3
      The question in #2 wasn't answered but remains pertinent. If for example start is string then a guess is

      Code:
      gen year = real(substr(start, 1, 4))
      and if it is numeric a guess is

      Code:
      gen year = floor(start/10000)


      Comment


      • #4
        See also https://www.statalist.org/forums/for...year-from-date

        Comment

        Working...
        X