Announcement

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

  • STATA date format

    Dear All,

    Can you help me in converting this date format

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str255 yearofrecord
    "26/03/21"
    "25/03/21"
    "23/03/21"
    "23/03/21"
    "22/03/21"
    "22/03/21"
    "19/03/21"
    "18/03/21"
    "18/03/21"
    "16/03/21"
    "15/03/21"
    "13/03/21"
    "12/03/21"
    "09/03/21"
    "04/03/21"
    "03/03/21"
    "02/03/21"
    "02/03/21"
    "25/02/21"
    "19/02/21"
    "19/02/21"
    "18/02/21"
    "18/02/21"
    "17/02/21"
    "17/02/21"
    "16/02/21"
    "16/02/21"
    "16/02/21"
    "15/02/21"
    "10/02/21"
    "09/02/21"
    "08/02/21"
    "06/02/21"
    "06/02/21"
    "05/02/21"
    "05/02/21"
    "04/02/21"
    "04/02/21"
    "03/02/21"
    "03/02/21"
    "29/01/21"
    "29/01/21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    "1-Jul-21"
    end

    Convert this string variable into date and extract the year from date variable.

    Thank you

  • #2
    Thanks for the data example. We don't need the duplicates.

    If you all care about is the year, it's just the last two characters of the date string plus a century start. Alternatively, you can get a daily date and then pull the year out of that. Documentation at

    Code:
    help string functions 
    
    help date functions
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str255 yearofrecord
    "26/03/21"
    "25/03/21"
    "23/03/21"
    "22/03/21"
    "19/03/21"
    "18/03/21"
    "16/03/21"
    "15/03/21"
    "13/03/21"
    "12/03/21"
    "09/03/21"
    "04/03/21"
    "03/03/21"
    "02/03/21"
    "25/02/21"
    "19/02/21"
    "18/02/21"
    "17/02/21"
    "16/02/21"
    "15/02/21"
    "10/02/21"
    "09/02/21"
    "08/02/21"
    "06/02/21"
    "05/02/21"
    "04/02/21"
    "03/02/21"
    "29/01/21"
    "1-Jul-21"
    end
    
    .  gen year1 = 2000 + real(substr(year, -2, 2))
    
    . gen date = daily(yearofrecord, "DMY", 2025)
    
    . format date %td
    
    . gen year2 = year(date)
    
    . assert year1 == year2
    
    . list
    
         +--------------------------------------+
         | yearof~d   year1        date   year2 |
         |--------------------------------------|
      1. | 26/03/21    2021   26mar2021    2021 |
      2. | 25/03/21    2021   25mar2021    2021 |
      3. | 23/03/21    2021   23mar2021    2021 |
      4. | 22/03/21    2021   22mar2021    2021 |
      5. | 19/03/21    2021   19mar2021    2021 |
         |--------------------------------------|
      6. | 18/03/21    2021   18mar2021    2021 |
      7. | 16/03/21    2021   16mar2021    2021 |
      8. | 15/03/21    2021   15mar2021    2021 |
      9. | 13/03/21    2021   13mar2021    2021 |
     10. | 12/03/21    2021   12mar2021    2021 |
         |--------------------------------------|
     11. | 09/03/21    2021   09mar2021    2021 |
     12. | 04/03/21    2021   04mar2021    2021 |
     13. | 03/03/21    2021   03mar2021    2021 |
     14. | 02/03/21    2021   02mar2021    2021 |
     15. | 25/02/21    2021   25feb2021    2021 |
         |--------------------------------------|
     16. | 19/02/21    2021   19feb2021    2021 |
     17. | 18/02/21    2021   18feb2021    2021 |
     18. | 17/02/21    2021   17feb2021    2021 |
     19. | 16/02/21    2021   16feb2021    2021 |
     20. | 15/02/21    2021   15feb2021    2021 |
         |--------------------------------------|
     21. | 10/02/21    2021   10feb2021    2021 |
     22. | 09/02/21    2021   09feb2021    2021 |
     23. | 08/02/21    2021   08feb2021    2021 |
     24. | 06/02/21    2021   06feb2021    2021 |
     25. | 05/02/21    2021   05feb2021    2021 |
         |--------------------------------------|
     26. | 04/02/21    2021   04feb2021    2021 |
     27. | 03/02/21    2021   03feb2021    2021 |
     28. | 29/01/21    2021   29jan2021    2021 |
     29. | 1-Jul-21    2021   01jul2021    2021 |
         +--------------------------------------+
    See also https://www.statalist.org/forums/help#spelling

    Comment


    • #3
      Thank you . It works great

      Comment

      Working...
      X