user42459
user42459

Reputation: 915

Stata type mismatch, and no report of line number where error happened

I googled about "type mismatch", and it seems the errors mostly come from "replace"

Indeed I am doing some replacing but I can't see where that error comes from.

generate price=0.0
replace price=105.17 if year==2014

gen crisis=1 if year==2008 | year==2009
replace crisis=0 if year<2008 | year>2009

gen postcrisis=1 if year>2008
replace postcrisis=0 if year<=2008

Also, Stata isn't displaying at which line the error happened. This is very bad for debugging. How can I make it?

======================================

The error was coming from

generate realsales=sales/price

To see what is going wrong, I did the following.

. describe sales price

              storage   display    value
variable name   type    format     label      variable
>  label
------------------------------------------------------
sales           str8    %9s                   
price           float   %9.0g         

And destring didn't work.

. destring sales, replace
sales contains nonnumeric characters; no replace

Also, dataex didn't work.

. dataex
input statement exceeds linesize limit. Try specifying fewer variables

And still, when Stata stops with an error, it never tells me which line is causing the error. It simply shows me the following lines.

112. 
. }

(146 vars, 10748 obs)
type mismatch
r(109);

end of do-file

r(109);

This is very inconvenient for debugging. Is it really like this? Is there any way to make Stata display the error line?

Upvotes: 1

Views: 12238

Answers (1)

Nick Cox
Nick Cox

Reputation: 37208

In turn as you tell us nothing about your variables, this isn't a reproducible example.

A type mismatch means that you trying to do something numeric to strings, or vice versa. In your examples, possibly year is a string variable somehow. If so,

destring year, replace 

On debugging: Stata will stop with an error message as soon as it hits a problem. Otherwise, help trace to find out about program tracing.

Your example statements could all be condensed. In the last example, if crisis years are 2008 and 2009, you don't mean what you say.

generate price = cond(year == 2014, 105.17, 0) 

gen crisis = year==2008 | year==2009

gen postcrisis = year > 2009 

Upvotes: 1

Related Questions