废日期来自年份和月份功能

时间:2019-01-13 15:01:24

标签: stata

我有一个birthtime变量:

birthtime 
1976m2
1979m8

我运行了以下内容:

gen birthyear=year(birthtime)
gen birthmonth=month(birthtime)

但是,结果是胡说八道。

yearmonth函数不适用于此格式吗?

如何从birthtime变量中检索年份和月份?

2 个答案:

答案 0 :(得分:3)

您需要学习Stata的SIF-to-SIF conversions

clear

input birthtime
403
404
405
406
407
end

generate birthyear = year(dofm(birthtime))
generate birthmonth = month(dofm(birthtime))

list, abbreviate(10)

     +------------------------------------+
     | birthtime   birthyear   birthmonth |
     |------------------------------------|
  1. |       403        1993            8 |
  2. |       404        1993            9 |
  3. |       405        1993           10 |
  4. |       406        1993           11 |
  5. |       407        1993           12 |
     +------------------------------------+

也:

format %tm birthtime

list, abbreviate(10)

     +------------------------------------+
     | birthtime   birthyear   birthmonth |
     |------------------------------------|
  1. |    1993m8        1993            8 |
  2. |    1993m9        1993            9 |
  3. |   1993m10        1993           10 |
  4. |   1993m11        1993           11 |
  5. |   1993m12        1993           12 |
     +------------------------------------+

答案 1 :(得分:1)

我发现以下工作可以解决

gen int temp = dofm(birthtime)
gen int birthyear = year(temp)
gen byte birthmonth = month(temp)

结果是

birthyear birthmonth
1976         2
1979         8