Announcement

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

  • Numeric Date Variable Read as String Variable

    Hi All,

    I am using Stata 13. I have a numeric birth date variable that Stata is reading as string (type str10 format %10s). I am importing the data set from excel. I have tried the destring command and Stata returns this message "chd_b_date contains nonnumeric characters; no replace". This is an example of how birth date is inputed in my data set 12/31/2002. I have another data set that I import into Stata and the same birth date variable is read as "int" for type and "%tdnn/dd/CCYY" for format. How can I change the birth date variable that is being read as string to this type "int" and this format "%tdnn/dd/CCYY".

    I looked at this forum http://www.statalist.org/forums/foru...e-output/page2 and a PDF from the Stata Journal "Cleaning up user entered string variables". Those resources had me try to delete blank spaces in cells but nothing has worked so far. I used
    Code:
    gen chd_b_date1=trim(chd_b_date)
    and the new variable also cannot be changed to numeric.
    I tried this code to see if I can identify which cells are causing problems
    Code:
    list chd_b_date if missing(real(chd_b_date))
    . Stata listed all the observations again this is an example of how birth date is entered 11/14/2006.

    Any comments or support that you can share will be much appreciated!
    Thank you
    Patrick

  • #2
    Patrick,

    Try using the date() function:

    Code:
    gen chd_b_date1=date(chd_b_date,"MDY")
    Regards,
    Joe

    Comment


    • #3
      -destring-, were you to force it to run, would not convert "11/14/2006" into the Stata date value corresponding to 14November2006. It would convert it into something like 11,142,006, which will not match with your other date variable, and will not be useful for anything really.

      You need to do
      Code:
      gen chd_b_date1 = date(chd_b_date, "MDY")
      Note: you should really read the online manual about date variables and date functions. Learning to work with dates in Stata has a fairly steep learning curve.

      Comment


      • #4
        "a fairly steep learning curve"

        That means: you put in a little effort, you learn a lot. But, as Clyde implied, you do need to read the help here to avoid getting confused.

        Comment


        • #5
          Well that was easy, thanks Joe!

          Comment

          Working...
          X