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

  • type mismatch error when using "replace" function

    Hi everyone,

    I am using STATA version 14.0 and am hoping to replace missing observations using:
    "replace state = . if state == -3 Missing geocode".
    Unfortunately, I keep receiving the error "type mismatch". The variable I am working with is categorical and I need to ensure that I am not analyzing any data that is missing. Thank you!

  • Nick Cox
    Rich Goldstein gave good advice necessarily limited by the failure of #1 to include a decent data example. You should please install and use dataex as explained in FAQ Advice #12.

    My guess is different: you need to map e.g. -3 to some code like .c but I won’t guess much at code without confirmation of how your data show values that mean missing in any sense. Either way if state is numeric then the replace command (not a function) would need to be more like

    replace state = .c if state == -3

    On this guess the error message arises because you included the value label in your statement.

    All that said, you would be better off with a more systematic remapping such as that possible with recode.

    Leave a comment:

  • Rich Goldstein
    is the actual value of state "-3 Missing geocode" in at least one case (note that if you had used -dataex- as explained in the FAQ, there would be no need for me to ask this; if yes, then there are several routes possible (as the variable is currently a string) and which is correct depends on whether there are other "odd" exaples; if no, then I am really confused; at a guess:
    replace state="" if state=="-3 Missing geocode"
    then. if you want the variable to be numeric, you can use -destring-; if you feel comfortable that this is the only problem, then you can go directly to -destring- (with the "force" option)

    as I recall, there were free updates to version 14 that you can, and should, install; see
    help update

    Leave a comment: