Announcement

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

  • Transformation of date into year

    Hi,

    in my dataset I have the variable "annualreportdate" indicating at what date the report was written/pubished, written as dmmyy. Since it is always published at the first of a month there are not 2 digits for the day.
    I now want to get the year in order to merge it to another dataset.
    I already generated a string variable containing these information (annualreportdate_string).
    But I am not able to now apply these commands:

    gen date = date(annualreportdate_string, "DMY")
    gen date = date(annualreportdate_string, "DM20Y")
    gen year = yofd(annualreportdate)

    Can you tell me how I am able to just get the year as variable out of the given date?

    Thank you very much for your help.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int annualreportdate str5 annualreportdate_string
    10320 "10320"
    10319 "10319"
    10318 "10318"
    10317 "10317"
    10316 "10316"
    end

  • #2
    You can actually get the YY year directly from the original variable using the mod() function, and you can then figure out the four digit year using something like this:
    Code:
    gen byte yy = mod(annualreportdate,100)
    gen int year = cond(yy>2022 & !missing(yy), 1900+yy, 2000+yy)

    Comment


    • #3
      Thank you so much! This code worked for me and you literally saved my saturday!

      Comment

      Working...
      X