我如何生成年龄类别?我的 PATIENT_YOB 被指定为 01jan1956,我想知道确切的年龄

问题描述

我正在尝试使用以下代码,但出现错误

 01jan1986 
05jan2001 
07mar1983 
and so on I need to get the exact age of them
gen agecat=1
if age 0-20==1
if age 21-40==2
if age 41-60==3
if age 61-64==4```

解决方法

这是一种方法:

gen age_cat = cond(age <= 20,1,cond(age <= 40,2,cond(age <= 60,3,cond(age <= 64,4,.))))

您可能还想查看 egen,cut,请参阅 help egen

,

为了建立 Wouter 的答案,您可以执行以下操作来计算到 10 岁的年龄:

clear
set obs 12
set seed 12352

global today = date("18Jun2021","DMY")

* Sample Data
gen dob = runiformint(0,17000) // random Dates
format dob %td

* Create Age
gen age = round((ym(year(${today}),month(${today})) - ym(year(dob),month(dob)))/ 12,0.1)

* Correct age if dob in current month,but after today's date
replace age = age - 0.1 if (month(${today}) == month(dob)) & (day(dob) > day(${today}))

* age category
gen age_cat = cond(age <= 20,.))))

倒数第二步很重要,因为如果他们的出生日期与比较日期在同一个月但尚未实现,则它会递减年龄。


     +----------------------------+
     |       dob    age   age_cat |
     |----------------------------|
  1. | 30jan2004   17.4         1 |
  2. | 14aug1998   22.8         2 |
  3. | 06aug1998   22.8         2 |
  4. | 31aug1994   26.8         2 |
  5. | 27mar1990   31.3         2 |
     |----------------------------|
  6. | 12jun1968     53         3 |
  7. | 05may1964   57.1         3 |
  8. | 06aug1994   26.8         2 |
  9. | 21jun1989   31.9         2 |
 10. | 10aug1984   36.8         2 |
     |----------------------------|
 11. | 22oct2001   19.7         1 |
 12. | 03may1972   49.1         3 |
     +----------------------------+

请注意,小数只是近似值,因为它使用的是生日月份而不是实际日期。

,

您在其他答案中得到了一些很好的建议,但这可以随心所欲。

考虑这个例子,注意将数据表示为我们可以运行的代码是一个非常有用的细节。

* Example generated by -dataex-. For more info,type help dataex
clear
input str9 sdate float dob
"01jan1986"  9497
"05jan2001" 14980
"07mar1983"  8466
end
format %td dob

2020 年底的年龄就是 2020 减去人们的出生年份。如果更有意义,请使用任何其他年份。

. gen age = 2020 - year(dob)

. l

     +-----------------------------+
     |     sdate         dob   age |
     |-----------------------------|
  1. | 01jan1986   01jan1986    34 |
  2. | 05jan2001   05jan2001    19 |
  3. | 07mar1983   07mar1983    37 |
     +-----------------------------+

对于 20 年的垃圾箱,为什么不让它们具有自我描述性。因此,使用此代码,20、40 等是每个 bin 的上限。 (如果您的数据中有 1 岁以下的儿童,您可能需要对此进行调整。)

. gen age2 = 20 * ceil(age/20)

. l

     +------------------------------------+
     |     sdate         dob   age   age2 |
     |------------------------------------|
  1. | 01jan1986   01jan1986    34     40 |
  2. | 05jan2001   05jan2001    19     20 |
  3. | 07mar1983   07mar1983    37     40 |
     +------------------------------------+

This paper 是对使用 Stata 的舍入和分箱的回顾。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...