【问题标题】:How do I generate age category? My PATIENT_YOB is given as 01jan1956 and I want to get exact age如何生成年龄类别?我的 PATIENT_YOB 是 01jan1956,我想得到确切的年龄
【发布时间】:2021-09-03 03:40:34
【问题描述】:

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

 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```

【问题讨论】:

  • 我的 PATIENT_YOB 是 01jan1956,我想得到确切的年龄,然后生成分类变量(年龄猫)
  • Stata 17 中添加了年龄函数。如果您使用的是以前的版本,请说出来,否则什么定义年龄? 2020 年 12 月 31 日的年龄是最简单的,因为它只是 2020 - year(PATIENT_YOB) 假设后者是 Stata 每日日期变量l
  • 显然你的第一语言不是英语,我们理解,但我猜这被否决了,因为(1)它是一团糟(2)问题混合了数据和一些大部分不会的代码跑步;它看起来不像是认真的代码工作(3),只是在 cmets 中重复你的问题真的没有帮助。人们想提供帮助,但请帮助我们帮助您。

标签: windows stata stata-macros statamic readstata13


【解决方案1】:

这是一种方法:

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

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

【讨论】:

    【解决方案2】:

    为了建立 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, 1, cond(age <= 40, 2, cond(age <= 60, 3, cond(age <= 64, 4, .))))
    

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

    
         +----------------------------+
         |       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 |
         +----------------------------+
    

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

    【讨论】:

      【解决方案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 进行舍入和分箱的回顾。

      【讨论】:

        猜你喜欢
        • 2019-10-31
        • 1970-01-01
        • 1970-01-01
        • 2020-10-08
        • 2018-10-29
        • 2019-07-10
        • 2021-12-08
        • 2020-05-27
        • 1970-01-01
        相关资源
        最近更新 更多