Stata 每月函数生成缺少的值
我正在尝试使用Stata中的Stata 每月函数生成缺少的值,stata,Stata,我正在尝试使用Stata中的monthly()函数将一组月-年对从字符串转换为数字。此变量的数据如下所示: rcsbirth 072005 101993 012001 121994 我已经能够使用date()函数执行此操作: generate childbirth = date(rcsbirth,"MY") 接下来,我使用: format childbirth %td 变量分娩现在为相应的rcsbirth日期列出如下: 01jul2005 01oct1993 01jan2001 0
monthly()
函数将一组月-年对从字符串转换为数字。此变量的数据如下所示:
rcsbirth
072005
101993
012001
121994
我已经能够使用date()
函数执行此操作:
generate childbirth = date(rcsbirth,"MY")
接下来,我使用:
format childbirth %td
变量分娩
现在为相应的rcsbirth
日期列出如下:
01jul2005
01oct1993
01jan2001
01dec1994
当我尝试使用monthly()
函数应用这个非常相同的过程,并使用%tm
而不是date()
和%td
进行格式化时,它会在新的分娩
变量中生成所有缺失的值
我再次尝试申请:
generate childbirth = monthly(rcsbirth, "MY")
我确认这个问题,无法解释 这里有两种方法可以从示例数据中获得一行中的每月日期
input str6 rcsbirth
"072005"
"101993"
"012001"
"121994"
end
generate childbirth = ym(real(substr(rcsbirth,-4,4)), real(substr(rcsbirth, 1, 2)))
generate childbirth2 = mofd(date(rcsbirth, "MY"))
format childbirth* %tm
list
+--------------------------------+
| rcsbirth childb~h childb~2 |
|--------------------------------|
1. | 072005 2005m7 2005m7 |
2. | 101993 1993m10 1993m10 |
3. | 012001 2001m1 2001m1 |
4. | 121994 1994m12 1994m12 |
+--------------------------------+
日期时间翻译帮助文件(
帮助日期时间翻译
)说明:
“…monthly()转换一年和一个月的数字(1-12)
因此,以下方法有效:
generate wanted = monthly(substr(rcsbirth, 1, 2) + " " + substr(rcsbirth, 3, .), "MY")
format wanted %tm
list
+--------------------+
| rcsbirth wanted |
|--------------------|
1. | 072005 2005m7 |
2. | 101993 1993m10 |
3. | 012001 2001m1 |
4. | 121994 1994m12 |
+--------------------+
换句话说,月份必须以年分隔,并用空格隔开,-
或/