Reputation: 85
I am attempting a simple replacement of the string "Female" with dummy values, 1 & 0. I understand that transforming from string is not allowed. So I attempted to make the conversion in the following way:
replace female =1 if female=="Female"
and this way
replace female = "1" if female == "Female"
but both spit out the error term:
type mismatch
r(109);
I'm not sure how to fix this, I was sure the second code listed would work.
Upvotes: 0
Views: 1051
Reputation: 37358
The implication is that the variable female
, contrary to your report, is numeric. It may have value labels attached.
You don't give a data example, or even the report from a describe
, but both errors can be reproduced.
. clear
. set obs 2
number of observations (_N) was 0, now 2
. gen female = cond(_n == 1, 1, 0)
. label def female 1 female 0 male
. label val female female
. list
+--------+
| female |
|--------|
1. | female |
2. | male |
+--------+
. replace female = "1" if female == "female"
type mismatch
r(109);
. replace female = 1 if female == "female"
type mismatch
r(109);
. d female
storage display value
variable name type format label variable label
-------------------------------------------------------------------------------------------------------------
female float %9.0g female
The reason for failure is different, even though the error message is the same.
. replace female = "1" if female == "female"
type mismatch
r(109);
This fails because you are trying to replace the value of female
with the string "1"
, but female
is a numeric variable. Stata does not get as far as the comparison of female
with the string "female"
, which would fail for the same reason.
. replace female = 1 if female == "female"
type mismatch
r(109);
Trying to replace female
with the numeric value 1
is fine, but the test if female == "female"
is out of order because the variable female
is numeric.
There is still the question of what you are trying to do and why. From a Stata point of view, an ideal indicator variable
(a) is 1 or 0
(b) has value labels assigned
(c) is named for the state with value 1, so is named female
if and only the value 1 means female.
Hence it may be that you don't need to do anything here.
I've heard repeated stories of presenters being asked which way their indicators gender
or sex
went and not knowing, which is a serious gaffe. Much more discussion can be found in this paper.
Upvotes: 2