Announcement

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

  • convert strings into date variables with very heterogeneous formats

    Hello dear community!

    I am referring to the attached screenshort where you see two columns containing start and end dates with very hetereogeneous formatting. As of now, both columns are stored as strings since "UnKnown" was inserted everytime no date is given. I am now (1) trying to convert these strings to dates and (2) seeking to extract the year from all these dates. Step 2 is fairly straightforward, but I am struggling with the conversion. There are three different formats: e.g. a.) 18.Jul 14; b.) 1995 and c.) Feb 73. Only the last two digits of the years are shown in cases a.) and c.) so that I would need insert "20" whenever the number are [0,19] and a "19" otherwise. I have been experimenting a lot with date() but failed as of now.

    Did someone already encounter a similar problem and could provide me with some insight?

    Thanks!

    Marie
    Attached Files

  • #2
    I solved it unconventionally with: gen startyear = substr(startdate,-2,.) extracting the last two digits from the string and then converting these into numbers. From there I simply did: gen year2 = cond(startyear1 < 20, 2000 + startyear1, 1900 + startyear1). Solved it perfectly! Just wanted to share my solution in case anyone else is facing sth similar.
    Last edited by Marie Hutter; 06 Apr 2019, 12:36.

    Comment


    • #3
      Thank you for closing the thread by sharing your solution.

      Comment

      Working...
      X