Announcement

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

  • Converting numeric date variable to string variable

    Dear all,
    I am very new to Stata.
    I want to convert the numeric date variable "cdint" (type: long, format: %tdD_m_Y) to a string variable.
    It currently looks like that: cdint
    25 Jan 07
    29 Jan 07
    13 Jan 07
    29 Jan 07
    04 Feb 07
    24 Jan 07
    01 Feb 07
    13 Jan 07
    26 Jan 07
    21 Jan 07
    25 Jan 07
    13 Jan 07
    15 Jan 07
    14 Jan 07
    12 Jan 07
    23 Jan 07
    05 Feb 07
    27 Jan 07
    27 Jan 07
    13 Jan 07
    12 Jan 07


    I tried to destring it and use the date command but Stata always generates missing values.

    destring cdint, replace
    gen date = date(cdint, "DMY")
    (3,176 missing values generated)


  • #2
    Welcome to Statalist.

    Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

    All Stata manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

    With that said, this problem was that Stata did not recognize "07" as a year, as one of the examples in the output of help datetime describes. The sample code shown below corrects the problem.
    Code:
    . * Example generated by -dataex-. To install: ssc install dataex
    . clear
    
    . input str9 cdint
    
             cdint
      1. "5 Jan 07" 
      2. "29 Jan 07"
      3. "13 Jan 07"
      4. "29 Jan 07"
      5. "04 Feb 07"
      6. "24 Jan 07"
      7. "01 Feb 07"
      8. "13 Jan 07"
      9. "26 Jan 07"
     10. "21 Jan 07"
     11. "25 Jan 07"
     12. "13 Jan 07"
     13. "15 Jan 07"
     14. "14 Jan 07"
     15. "12 Jan 07"
     16. "23 Jan 07"
     17. "05 Feb 07"
     18. "27 Jan 07"
     19. "27 Jan 07"
     20. "13 Jan 07"
     21. "12 Jan 07"
     22. end
    
    . gen date = date(cdint, "DM20Y")
    
    . format date %td
    
    . list, clean
    
               cdint        date  
      1.    5 Jan 07   05jan2007  
      2.   29 Jan 07   29jan2007  
      3.   13 Jan 07   13jan2007  
      4.   29 Jan 07   29jan2007  
      5.   04 Feb 07   04feb2007  
      6.   24 Jan 07   24jan2007  
      7.   01 Feb 07   01feb2007  
      8.   13 Jan 07   13jan2007  
      9.   26 Jan 07   26jan2007  
     10.   21 Jan 07   21jan2007  
     11.   25 Jan 07   25jan2007  
     12.   13 Jan 07   13jan2007  
     13.   15 Jan 07   15jan2007  
     14.   14 Jan 07   14jan2007  
     15.   12 Jan 07   12jan2007  
     16.   23 Jan 07   23jan2007  
     17.   05 Feb 07   05feb2007  
     18.   27 Jan 07   27jan2007  
     19.   27 Jan 07   27jan2007  
     20.   13 Jan 07   13jan2007  
     21.   12 Jan 07   12jan2007  
    
    .

    Comment


    • #3
      Thank you very much, William!
      It worked.

      Comment

      Working...
      X