Announcement

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

  • Extracting a part of a string variable

    Dear all,

    I have just started working with stata and I want to extract the year of a date variable which is saved as a string (e. g. "17.12.2019"). I do not know which command is needed and I am also not too familiar with the syntax of stata.

    I am grateful for any assistance.

    Kind regards,
    Eric

  • #2

    Code:
    . di real(substr("17.12.2019", -4, 4))
    2019
    For a variable that would be something like

    Code:
    gen year = real(substr(trim(yourdate), -4, 4)
    where the trim() would take care of any leading or trailing spaces.

    See

    Code:
    help string functions 
    
    help datetime

    Comment


    • #3
      Thanks a lot, Nick.

      Comment

      Working...
      X