Announcement

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

  • Type mismatch error even though both type is float

    Why do I have type mismatch error even though types of two variable co_start_date and co_end_date are the same?



    . foreach v of varlist co_start_date co_end_date {
    2. gen _`v' = daily(`v', "DMY"), after(`v')
    3. assert missing(`v') == missing(_`v')
    4. drop `v'
    5. rename _`v' `v'
    6. format `v' %td
    7. }
    type mismatch
    r(109);




    . dataex YEAR GVKEY EXECID co_start_date co_end_date end_date

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double YEAR str6(GVKEY EXECID) float(co_start_date co_end_date end_date)
    2002 "119417" "00008"  3408 15341 15341
    2003 "6307"   "00008"  3408 15706 15706
    2002 "4598"   "00016" -1767 12692 15341
    2003 "157415" "00016" -1767 12692 15706
    2004 "10920"  "00016" -1767 12692 16071
    2002 "10498"  "00026" 11504 15522 15341
    2002 "148294" "00042"   366 15341 15341
    2003 "24607"  "00042"   366 15706 15706
    2008 "3662"   "00042"   366 17532 17532
    2009 "7435"   "00042"   366 17898 17898
    2002 "24607"  "00043"  3804 15096 15341
    2002 "11456"  "00062" -1095 15341 15341
    2003 "147885" "00062" -1095 15521 15706
    2004 "15444"  "00062" -1095 15521 16071
    2005 "4598"   "00062" -1095 15521 16437
    2002 "7139"   "00066"  6118 15341 15341
    2003 "10920"  "00066"  6118 15706 15706
    2004 "25777"  "00066"  6118 15796 16071
    2002 "25313"  "00074"  8248 15341 15341
    2006 "62374"  "00075" 17015 16802 16802
    2002 "28930"  "00116" 12585 15341 15341
    2003 "11259"  "00116" 12585 15706 15706
    2004 "5680"   "00116" 12585 16071 16071
    2005 "2721"   "00116" 12585 16437 16437
    2002 "64902"  "00117"  4169 15341 15341
    2003 "3138"   "00117"  4169 15706 15706
    2004 "11649"  "00117"  4169 15796 16071
    2005 "179741" "00117"  4169 15796 16437
    2006 "3362"   "00117"  4169 15796 16802
    2007 "30170"  "00117"  4169 15796 17167
    2008 "10005"  "00117"  4169 15796 17532
    2009 "10920"  "00117"  4169 15796 17898
    2010 "5680"   "00117"  4169 15796 18263
    2011 "112168" "00117"  4169 15796 18628
    2002 "3813"   "00125"  1827 15341 15341
    2003 "2393"   "00125"  1827 15706 15706
    2004 "10920"  "00125"  1827 16071 16071
    2005 "5680"   "00125"  1827 16437 16437
    2006 "10983"  "00125"  1827 16802 16802
    2007 "17239"  "00125"  1827 17167 17167
    2008 "133367" "00125"  1827 17532 17532
    2009 "11264"  "00125"  1827 17898 17898
    2010 "10443"  "00125"  1827 18263 18263
    2011 "24610"  "00125"  1827 18628 18628
    2002 "10920"  "00134"  9375 15341 15341
    2003 "2710"   "00134"  9375 15706 15706
    2004 "65702"  "00134"  9375 16071 16071
    2005 "241216" "00134"  9375 16437 16437
    2006 "5680"   "00134"  9375 16802 16802
    2007 "3144"   "00134"  9375 17167 17167
    2008 "5680"   "00134"  9375 17532 17532
    2002 "2193"   "00172"  4018 15341 15341
    2003 "16603"  "00172"  4018 15706 15706
    2005 "6307"   "00172"  4018 16437 16437
    2006 "10795"  "00172"  4018 16802 16802
    2007 "1722"   "00172"  4018 16908 17167
    2008 "6349"   "00172"  4018 16908 17532
    2002 "15444"  "00182"  9191 15341 15341
    2003 "8215"   "00182"  9191 15706 15706
    2002 "10405"  "00210" -1095 12996 15341
    2003 "5680"   "00210" -1095 12996 15706
    2004 "148256" "00210" -1095 12996 16071
    2005 "10920"  "00210" -1095 12996 16437
    2002 "1677"   "00229"  1277 15341 15341
    2003 "175689" "00229"  1277 15706 15706
    2004 "6829"   "00229"  1277 16071 16071
    2005 "24607"  "00229"  1277 16437 16437
    2006 "4598"   "00229"  1277 16802 16802
    2007 "11456"  "00229"  1277 17167 17167
    2002 "11456"  "00260"   731 13264 15341
    2003 "5680"   "00260"   731 13264 15706
    2004 "160163" "00260"   731 13264 16071
    2005 "64389"  "00260"   731 13264 16437
    2002 "6649"   "00281"  8887 15341 15341
    2003 "8214"   "00281"  8887 15706 15706
    2004 "10920"  "00281"  8887 16071 16071
    2005 "10920"  "00281"  8887 16437 16437
    2006 "62374"  "00281"  8887 16802 16802
    2007 "10920"  "00281"  8887 17167 17167
    2008 "3505"   "00281"  8887 17532 17532
    2002 "146500" "00296"  9313 15341 15341
    2003 "5071"   "00296"  9313 15706 15706
    2004 "6829"   "00296"  9313 16071 16071
    2005 "6829"   "00296"  9313 16437 16437
    2006 "4213"   "00296"  9313 16802 16802
    2007 "10920"  "00296"  9313 17167 17167
    2008 "8717"   "00296"  9313 17532 17532
    2009 "10816"  "00296"  9313 17898 17898
    2010 "6104"   "00296"  9313 18263 18263
    2011 "3138"   "00296"  9313 18628 18628
    2002 "24931"  "00297"  1827 15341 15341
    2002 "24607"  "00325" 10409 15341 15341
    2003 "8463"   "00325" 10409 15706 15706
    2004 "4598"   "00325" 10409 16071 16071
    2005 "150481" "00325" 10409 16437 16437
    2002 "62865"  "00339" 12419 15341 15341
    2003 "7486"   "00339" 12419 15706 15706
    2002 "5071"   "00347" 14835 15341 15341
    2003 "5680"   "00347" 14835 15706 15706
    2004 "8215"   "00347" 14835 16071 16071
    end
    format %td co_start_date
    format %td co_end_date
    format %td end_date
    ------------------ copy up to and including the previous line ------------------

    Listed 100 out of 44487 observations
    Use the count() option to list more

  • #2
    The type mismatch arises from
    Code:
    gen _`v' = daily(`v', "DMY"), after(`v')
    because the first argument of the -daily()- function must be a string variable.

    In fact, the purpose of the code you show is to convert string variables that read as dates to humans into Stata internal format numeric date variables. But in this data set, co_start_date and co_end_date are starting out as Stata internal format numeric date variables already. So the entire block of code serves no purpose with this data. I think you have copied that code from some earlier problem where the date variables started out as strings.

    Comment


    • #3
      Both co_start_date co_end_date are str variables but after I run the foreach loop, why co_start_date changes to float type?? after showing the error message below



      . foreach v of varlist co_start_date co_end_date {
      2. gen _`v' = daily(`v', "DMY"), after(`v')
      3. assert missing(`v') == missing(_`v')
      4. drop `v'
      5. rename _`v' `v'
      6. format `v' %td
      7. }
      (39 missing values generated)
      39 contradictions in 24,526 observations
      assertion is false

      Comment


      • #4
        Originally posted by BB Kim View Post
        Both co_start_date co_end_date are str variables but after I run the foreach loop, why co_start_date changes to float type??
        It doesn't.

        . quietly set obs 1

        . generate str co_start_date = "happy"

        . generate int _co_start_date = daily(co_start_date, "DMY")
        (1 missing value generated)

        . assert missing(_co_start_date) == missing(co_start_date)
        assertion is false
        r(9);

        . display in smcl as text "`: type co_start_date'"
        str5

        .



        The problem is that 39 observations' worth of one of your two string date variables contains invalid dates and cannot be converted. Take a look at them with the following and you'll see what I mean.
        Code:
        list if missing(_co_start_date) != missing(co_start_date) | ///
            missing(_co_end_date) != missing(co_end_date)
        Last edited by Joseph Coveney; 04 Mar 2023, 20:01.

        Comment


        • #5
          Oh, actually you did drop co_start_date and rename the converted variable to its name.

          So, yes, it's float because you commanded Stata to make it so.

          Because your loop made it that far (renaming _co_start_date to co_start_date), it's your second variable, co_end_date, that has 39 invalid string date values.

          Comment


          • #6
            How can I change the code if I don't want to change it to float?

            Comment


            • #7
              Below is my whole code, and since 39 invalid string date values, forvalues loop in the end shows "if not find" error message
              Which part of the code should I change?




              foreach v of varlist co_start_date co_end_date {
              gen _`v' = daily(`v', "DMY"), after(`v')
              assert missing(`v') == missing(_`v')
              drop `v'
              rename _`v' `v'
              format `v' %td
              }


              local recession1 August 1929 March 1933
              local recession2 May 1937 June 1938
              local recession3 February 1945 October 1945
              local recession4 November 1948 October 1949
              local recession5 July 1953 May 1954
              local recession6 August 1957 April 1958
              local recession7 April 1960 February 1961
              local recession8 December 1969 November 1970
              local recession9 November 1973 March 1975
              local recession10 January 1980 July 1980
              local recession11 July 1981 November 1982
              local recession12 July 1990 March 1991
              local recession13 March 2001 November 2001
              local recession14 December 2007 June 2009
              local recession15 February 2020 April 2020

              // CLEAN UP THE RECESSION LOCALS AND MAKE THEM NUMERIC
              forvalues i = 1/15 {
              local m1: word 1 of `recession`i''
              local y1: word 2 of `recession`i''
              local m2: word 3 of `recession`i''
              local y2: word 4 of `recession`i''
              local recession_start`i' = dofm(monthly("`m1' `y1'", "MY"))
              local recession_end`i' = dofm(monthly("`m2' `y2'", "MY"))
              assert !missing(`recession_start`i'', `recession_end`i'')
              }

              gen first_co_r_worked_start = .
              gen first_co_r_worked_end = .
              format first_co_r_worked* %td
              gen n_co_recessions_worked = 0

              forvalues i = 1/15 {
              replace first_co_r_worked_start = `recession_start`i'' if min(co_end_date, `recession_end`i'') >= max(co_start_date, `recession_start`i'') & missing(first_co_r_worked_start)
              replace first_co_r_worked_end = `recession_end`i'' if min(co_end_date, `recession_end`i'') >= max(co_start_date, `recession_start`i'') & missing(first_co_r_worked_end)
              replace n_co_recessions_worked = n_co_recessions_worked + 1 if min(co_end_date, `recession_end`i'') >= max(co_start_date, `recession_start`i'')
              }

              Comment


              • #8
                Which part of the code should I change?

                You shouldn't change any part of the code. The problem is with your data, not with the code. You have 39 values of co_end_date that are not actually possible dates. You need to fix those and then re-run everything from the beginning.

                Comment

                Working...
                X