Announcement

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

  • Type Mismatch when using "replace" command

    Hi, I am trying to create a new variable and then replace the values with the values of another variable given that that observation meets specific specifications. However, I am getting a type mismatch.

    gen femedc = .
    replace femedc = hv106_05 if hv101_05 == "Co-spouse" | hv101_05 == "Wife or husband" | hv101_05 == "co-spous" | hv101_05 == "wife or"

    femedc is my new variable. hv106_05 is the education of the fifth member in a household on a categorical scale. hv101_05 is the relationship of the house member to the household head. I want to replace femedc with the value of hv106_05 if that house member is the spouse of the household head. Does anyone know how to solve this problem?
    Thank you in advance!

    Andrew Hargrove

  • #2
    Your code implies that

    hv101_05

    is a string variable. The error message implies that it is not.

    If you present a data example as suggested -- see http://www.statalist.org/forums/help#stata -- then we can check exactly what is going on.

    Incidentally, your data probably need some cleaning as I would guess that

    Co-spouse
    co-spous


    mean the same and that you are not treating differences in spelling as informative.
    Last edited by Nick Cox; 17 Sep 2016, 11:07.

    Comment


    • #3
      hv101_05 is a string variable that has many possible categories: I am only concerned with 4 ("Wife or husband", "Co-spouse", "wife or", and "co-spous"). I only want femedc to be replaced if hv101_05 takes one of those four values. Also hv106_05 is a string variable as well. Could that be the problem? Do I need to code hv106_05 as a float variable?

      hv101_05 | Freq. Percent Cum.
      -------------------------------+-----------------------------------
      98 | 1 0.00 0.00
      99 | 12 0.00 0.00
      Adopted/foster child | 2,497 0.67 0.67
      Adopted/foster child/stepchild | 137 0.04 0.71
      Aunts/uncle | 11 0.00 0.71
      Brother/sister | 2,609 0.70 1.40
      Brother/sister-in-law | 99 0.03 1.43
      Co-spouse | 46 0.01 1.44
      Domestic servant | 30 0.01 1.45
      Don't know | 3 0.00 1.45
      Grand parents | 6 0.00 1.45
      Grandchild | 11,031 2.94 4.39
      Head | 8 0.00 4.39
      Maid | 39 0.01 4.40
      Niece/nephew by blood | 350 0.09 4.50
      Not related | 1,966 0.52 5.02
      Other relative | 6,233 1.66 6.68
      Parent | 1,477 0.39 7.07
      Parent-in-law | 622 0.17 7.24
      Son/daughter | 71,043 18.92 26.16
      Son/daughter-in-law | 2,065 0.55 26.71
      Wife or husband | 2,075 0.55 27.27
      adopted/ | 3,649 0.97 28.24
      brother/ | 6,540 1.74 29.98
      co-spous | 6 0.00 29.98
      dk | 19 0.01 29.99
      domestic | 40 0.01 30.00
      don't kn | 4 0.00 30.00
      grandchi | 37,717 10.05 40.05
      head | 9 0.00 40.05
      herdboy | 65 0.02 40.07
      house ma | 4 0.00 40.07
      in-house | 104 0.03 40.10
      maid / d | 57 0.02 40.11
      niece/ n | 302 0.08 40.19
      niece/ne | 5,096 1.36 41.55
      not rela | 4,397 1.17 42.72
      other re | 12,684 3.38 46.10
      parent | 4,930 1.31 47.41
      parent-i | 2,170 0.58 47.99
      parents/ | 51 0.01 48.00
      related | 12 0.00 48.01
      sister b | 367 0.10 48.10
      son/daug | 192,842 51.37 99.48
      step-son | 20 0.01 99.48
      tenant | 21 0.01 99.49
      uncle/au | 539 0.14 99.63
      wife or | 1,389 0.37 100.00
      -------------------------------+-----------------------------------
      Total | 375,394 100.00

      Comment


      • #4
        Sorry; I didn't notice another (in fact the) problem, but it's now clear..

        If hv106_05 is string then you can't use it to replace a numeric variable. By initialising femedc to system missing . you created it as numeric.

        Code:
        drop femedc
        gen femedc = hv106_05 if inlist(hv101_05, "Co-spouse", "Wife or husband", "co-spous",  "wife or")
        would be one way to approach that. Note how I rewrote your condition.

        Comment


        • #5
          I solved my problem! Thank you!

          I simply used [gen femedc = ""] instead of [gen femedc = .]
          That way stata assumes that any changes to femedc will be string data, not float.

          Thank you for your help!

          Comment


          • #6
            Well, now it's clear where your problem is. When you write
            Code:
            gen femedc = .
            you are telling Stata to create a numeric variable called femedc (specifically a float).

            Once you've established femedc as a numeric variable, you cannot replace its value with those of a string variable. So, of course, because hv106_05 is, you say, a string,
            Code:
            replace femedc = hv106_05 if hv101_05 == "Co-spouse" | hv101_05 == "Wife or husband" | hv101_05 == "co-spous" | hv101_05 == "wife or"
            will lead to an error message about incompatible data types. You've given us no information about hv106_05 other than that it is a string. If it is a string variable whose content is actually numbers represented as strings, you might consider -destring-ing it to make it a numeric variable. (I would do this if, and only if, it is a variable whose numeric values will later be used in computations. If it's a numeric identifier like a social security number of a zipcode, I'd leave it as a string.) If you want to leave hv106_05 as a string, then when you generate femedc, you should create it as a string variable:
            Code:
            gen femedc = ""
            Also note that your -if- qualifer can be written more compactly as:
            Code:
            if inlist(hv101_05, "Co-spouse", "Wife or husband", "co-spous", "wife or")
            Added: Crossed with #5.

            Comment


            • #7
              http://www.stata.com/statalist/archi.../msg01079.html

              Comment

              Working...
              X