简体   繁体   English

如何生成 AGE 分类/二进制变量?

[英]How can I generate AGE categorical/binary variable?

I tried using the below code but it throws an error in Stata.我尝试使用以下代码,但它在 Stata 中引发错误。

gen agecat=.
replace agecat=1 if age<=20
replace agecat=2 if age==21-30
replace agecat=2 if age==31-40
replace agecat=2 if age>=64

Would this work?这行得通吗? If not, you must let us know what error you get.如果没有,您必须让我们知道您遇到了什么错误。 It is difficult for us to help you if we do not know what error you get.如果我们不知道您遇到什么错误,我们很难为您提供帮助。

gen     agecat = 1
replace agecat = 2 if age >  20
replace agecat = 3 if age >  30
replace agecat = 4 if age >= 64 

If the age variable has missing values you probably want to add replace agecat = . if missing(age)如果age变量缺少值,您可能想要添加replace agecat = . if missing(age) replace agecat = . if missing(age) . replace agecat = . if missing(age) Otherwise all missing values would also be in the last category as numeric missing in Stata is treated as infinite (arbitrarily large), which is certainly larger than 64.否则所有缺失值也将在最后一个类别中,因为 Stata 中的数字缺失被视为无限(任意大),它肯定大于 64。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Stata 中,如何将 coefplot 的均值跨分类变量分组? - In Stata, how can I group coefplot's means across categorical variable? 我正在尝试将写为“11jun1965”的出生年份转换为 Stata 中的年龄变量。 使用以下代码,我无法生成当前年龄 - I'm trying to convert birth year written as '11jun1965' to age variable in Stata. With the following code, I could not generate the current age 如何创建非互斥的分类变量? - How do I create a non mutually exclusive categorical variable? 如何生成一个新变量,该变量是不同年份相同变量的价值之差? - How can I generate a new variable that is the difference in value of the same variable across different years? 在绘制分类变量的图形时,如何删除最左边的零(在x轴上)? - How do I remove the leftmost zero (on the x-axis) when graphing a categorical variable? 如何在 Stata 中找到每个变量(连续变量和分类变量)中缺失值的比例? - How do I find the proportion of missing values in each variable (continuous and categorical) in Stata? 如何根据分类变量生成具有统计信息的表? - How to produce a table with statistics according to categorical variable? 生成分类变量 - Generating categorical variable #1 组合分类变量的类别 - #1 Combining categories of a categorical variable 重新编码分类变量(Stata) - Recoding Categorical Variable (Stata)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM