Announcement

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

  • Int variable to string results in strange numbers

    Hi,

    I have a date of birth variable in int form. For example an observation could be "07dec1978".
    I want to turn it into a string variable first and then draw out the first two characters out of the variable (07 in my example) by substr function.
    But when I run the tostring command, 07dec1978 becomes 6915!
    How is that possible and how can I fix this?

    Best

  • #2
    I think you are going about it the wrong way. See

    Code:
    help datetime
    In particular, the -day()- function is what you need.

    Code:
    di day(6915)
    Res.:

    Code:
    . di day(6915)
    7
    
    .

    Comment


    • #3
      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 and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

      Comment


      • #4
        As an extra but notable detail 6915 is a daily date, 7 December 1978, when so offered; and in reverse "6915" is a correct string rendering of that daily date -- unless you specify a daily date format to tostring.

        As explained -- see #3 -- daily dates are counted forwards and backwards from 0 as 1 January 1960.

        But day() is the direct solution. to the problem, as explained in #2.

        Comment

        Working...
        X