Announcement

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

  • generate year from DMY hms format

    Hello,

    I have a numeric time variable in a format of "DMY 00:00:00". I want to create a new variable that only contains the years of this time variable. Can anyone let me know what code should I use? Thanks!

  • #2
    you need to give us a little more info; however, here is an example assuming that the actual format is DDMMYYYY, etc:

    Code:
    gen year=real(substr(string(oldvar),5,4))
    leave off the "real" part if you want year to be a string variable

    Comment


    • #3
      If as I suspect the time variable is of the "datetime/c" type (see help datetime) you may want to read help year() and help dofc(), then try:
      Code:
      gen year=year(dofc(mydate))

      Comment


      • #4
        I use the code " year(dofc(mydate))". It works perfectly. Thanks a lot!

        Comment

        Working...
        X