Announcement

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

  • Generate and replace errors

    Hello, ultimately trying to take a categorical variable (9 categories) to 4 categories. I tried to generate a new variable and replace to code but I keep getting r(109) type mismatch.
    generate race=.
    (14,072 missing values generated)

    . replace race =1 if momrace==1
    type mismatch
    r(109);

    Thanks for any help!

  • #2
    Well, momrace must be a string variable. You can confirm this by running
    Code:
    des momrace
    If you are not planning to use momrace at all, or if you are content to use it as a string variable, then the simple fix is to just change the code to
    Code:
    replace race = 1 if momrace == "1"
    However, if you plan to use momrace later on and if you need it to be a numeric variable then you need to convert it. Assuming that all of the values of momrace look like 1, 2, etc. (although they are really strings "1", "2", etc.) then you would convert it by using
    Code:
    destring momrace, replace
    If you do that, then you can use your original -replace- command without modification.

    Comment

    Working...
    X