Announcement

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

  • Extract year from Date

    Hallo,

    I have have a little problem and would be thankful if anyone could help me.
    My dataset contains date in form of e.g. (19800229) and i want to extract the year (for this example 1980).

    I already tried the command
    gen fyear=year( fiscal_year_end)
    but I just get missing values.

    I think its because Stata can´t ready the format.

    It would be great if anyone can help me.
    Thanks in advance.

    Best regards
    Jana

  • #2
    If fiscal_year_end numeric and always 8 digits,
    Code:
    gen fyear = int(fiscal_year_end/10000)
    If fiscal_year_end numeric but not always 8 digits,
    Code:
    tostring(fiscal_year_end),replace
    gen fyear = real(substr(fiscal_year_end,1,4))
    If fiscal_year_end string,
    Code:
    gen fyear = real(substr(fiscal_year_end,1,4))

    Comment


    • #3
      See also https://www.statalist.org/forums/for...dates-to-years

      Comment


      • #4
        It worked.

        Thanks for your help!

        Comment

        Working...
        X