Announcement

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

  • Calculate the difference between the dates: string conversion

    Hello there,

    I have a column "begin" which corresponds to the date of the begin of parental leave and the column "end", which corresponds to the date when parental leave ended. I want to create variable which corresponds to the length of the parental leave, which is given by the difference between "end" and "begin" date.

    I first tried to change these float variables into strings and get rid of the the index in the brackets. I used these commands, but it did not work out:

    *Calculate the difference between the "begin_date" and "end_date" to find the length of parental leave

    * Ensure 'begin' and 'end' are strings
    gen begin_str = string(begin)
    gen end_str = string(end)

    * Identify the position of the closing bracket and space
    gen begin_pos = strpos(begin_str, "] ") + 2
    gen end_pos = strpos(end_str, "] ") + 2

    * Extract the date part using the identified positions
    gen begin_date_str = substr(begin_str, begin_pos, 8)
    gen end_date_str = substr(end_str, end_pos, 8)

    * Inspect the extracted date parts
    list begin end begin_date_str end_date_str in 1/10


    I did not obtain, what I wanted to, and therefore I can't calculate difference between the begin date and end date (see picture 2). Could you please help me in adjusting my code?

    Thank you!


    Screenshot 2024-08-08 at 15.08.10.png
    Screenshot 2024-08-08 at 18.21.24.png












    Attached Files
    Last edited by Lucy Triff; 08 Aug 2024, 10:27.
Working...
X