Announcement

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

  • generating dates/type mismatch (question from inexperienced user)

    Dear all,

    I am not very familiar with Stata yet and I was hoping you could help me with the following. I'm trying to subtract some dates and I'd like to start by creating a new date with this command:

    gen startdate1 = date(start1, "DMY")

    However I get the error 'type mismatch (r109). What does this mean? The data I use are in long format and look like 12-2-2009 (meaning 12 February 2009).

    Thanks!
    Lisanne Denneman

  • #2
    In the command line window, type describe start1. You'll see that the variable is numeric and is formatted as a date. The function date() takes a string date, such as "12-2-2009", and converts it into a Stata date (number of days since January 1st, 1960). That's already been done for you, and so you don't need to do the conversion. You can go ahead and do the subtractions on the start1 variable to subtract dates.

    Comment


    • #3
      Learning the terminology of new program is usually a pain. In this case your variable is of long storage type and has been assigned a date display format.

      If you had clicked on the part of the error message in blue you could have read this:

      [P] error . . . . . . . . . . . . . . . . . . . . . . . . Return code 109
      type mismatch;
      In an expression, you attempted to combine a string and numeric
      subexpression in a logically impossible way. For instance, you
      attempted to subtract a string from a number or you attempted
      to take the substring of a number.

      Comment


      • #4
        Thanks Nick & Joseph! However when I try to directly subtract the dates from each other with gen newdate = date1-date2 (both in long format), I get incorrect outcomes.
        For example 29-5-2006 minus 19-4-2006 becomes 22 (instead of 40 days); 10-6-1999 minus 22-4-1999 becomes -26 (instead of 48 days). How should I correct this?

        Comment


        • #5
          As already pointed out, long is a storage type, not a display format.

          We can't see how you created those variables. As you say, there must be a mistake somewhere as for example

          Code:
          . di date("29-5-2006", "DMY") - date("19-4-2006", "DMY")
          40
          Show us the results of

          Code:
          describe date1 date2
          l date1 date2 in 1/5
          format date1 date2 %10.0f
          l date1 date2 in 1/5
          and if you can explain exactly how those date variables were created (meaning, what was the source data and what were the Stata commands used).

          One wild thing that sometimes goes wrong is that people get mixed up between DMY and MDY. If they do that many dates are just coded as missings. Thus date("3-6-2014", "MDY") and date("3-6-2014", "DMY") are different. Referring to e.g. date("28-3-2014", "MDY") is not a syntax error, but just creates a missing value.

          That's one reason I often use display with little concrete examples to check I have the syntax right.

          So, check for missings in your date variables too.
          Last edited by Nick Cox; 11 Jun 2014, 04:38.

          Comment


          • #6
            1. The data are originally from an Excel file and I destringed them using the command

            encode start, generate (start1)

            Is that the problem?

            2. There is one date missing with the variable 'firstrna1'.

            3. These are the results (sorry I don't know how to copy it in a neat way):
            describe datefirstrna1 start1

            storage display value
            variable name type format label variable label
            ------------------------------------------------------------------------------------------------------------------------------------
            datefirstrna1 long %10.0g datefirstrna1

            start1 long %10.0g start1

            . l datefirstrna start1 in 1/5

            +------------------------+
            | datefir~a1 start1 |
            |------------------------|
            1. | 29-5-2006 19-4-2006 |
            2. | 25-1-2006 22-7-2005 |
            3. | 10-6-1999 22-4-1999 |
            4. | 13-10-2005 28-6-2005 |
            5. | 13-10-2005 27-8-2005 |
            +------------------------+

            . format datefirstrna1 start1 %10.0f

            . l datefirstrna1 start1 in 1/5

            +------------------------+
            | datefir~a1 start1 |
            |------------------------|
            1. | 29-5-2006 19-4-2006 |
            2. | 25-1-2006 22-7-2005 |
            3. | 10-6-1999 22-4-1999 |
            4. | 13-10-2005 28-6-2005 |
            5. | 13-10-2005 27-8-2005 |
            +------------------------+

            .
            end of do-file

            .

            Comment


            • #7
              Yes; we can stop at #1. encode is a completely inappropriate way to start creating dates. If you had just 3 string dates "12-12-2013" "23-01-1999" "28-03-1952" they would get mapped to 1, 2, 3 but what's insidious is that you should see value labels, which look as if you did the right thing. By default encode uses alphabetic order and that's quite useless for dates in general.

              Given a string date, use the date() function -- or its synonym daily() -- to create daily dates. Examples already in this thread.

              There are details in the FAQ Advice Section 12 on how to present code. You were asked to read it before posting!
              Last edited by Nick Cox; 11 Jun 2014, 05:15.

              Comment


              • #8
                Ok, thank you. And sorry for the code, I'll read the FAQ properly!

                Comment

                Working...
                X