1

I want to create a new variable in Stata age_cat based on age. My code:

gen age_cat = age
replace age_cat = "Adult" if age>18 | "0-5 years" if age >=0 & age<=5 | "6-11 years" if age>=6 & age<=11 | "12-18 years" if age>=12 & age<=18

I'm getting a type mismatch error:

type mismatch
r(109);

How do I fix this? I know encode will work for string to numeric replace. Is there a way to do this the other way around?

1
  • encode is not for string to numeric replace; it is for generating new variables. It's sometimes but not always the right thing to do if you have a string variable and want a numeric. Commented Aug 17, 2021 at 11:29

1 Answer 1

1

The immediate error is simple. You copied age to age_cat so age_cat is evidently a numeric variable, just like age. You then tried to replace it with string values. That's a type mismatch and Stata bailed out.

Alternatively, if age is really a string variable, then comparing it with numeric values such as 18 is also a type mismatch.

The rest of your syntax would have failed any way. The logical operator | (or) can be used to separate input conditions. It can never be used to separate different results.

Here is a slow way to do what you want, assuming age is numeric:

gen age_cat =  "Adult" if inrange(age, 19, .) 
replace age_cat = "12-18 years" if inrange(age, 12, 18)
replace age_cat = "6-11 years" if inrange(age, 6, 11) 
replace age_cat = "0-5 years" if inrange(age, 0, 5) 

I've added code such that missing ages, which count as more than 18, will not get classified as Adult.

Here's another way to do it:

gen age_cat = cond(age > 18, "Adult", cond(age >= 12, "12-18 years", cond(age >= 6, "6-11 years", "0-5 years))) if age < . 

where I have made a trap for missing values explicit. cond() is the equivalent of ifelse() in various languages. See also this tutorial if you want more detail.

There is also recode, which appeals to many people.

Sign up to request clarification or add additional context in comments.

1 Comment

Have always been a nested cond (or some egen function) over a recode guy myself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.