【问题标题】:How to calculate month between two dates in stata如何计算stata中两个日期之间的月份
【发布时间】:2017-04-18 17:17:48
【问题描述】:

我想创建一个带有信用年龄的变量。数据只有信用开始的日期。

我默认创建日期变量(例如 2017-12-31)。然后,我想用开始信用的日期计算年龄。

我试图搜索有关此的文章,但没有成功。

谢谢。

【问题讨论】:

  • 除了日期之外,我不明白这一点。我建议(请)(a)用你拥有的数据和你想要的数据给出一个工作示例(b)请一个英语更好的朋友重写:它不是语法或惯用的。使用dataex (ssc inst dataex)。假设不建议人们使用财务数据;假设除了 Stata 专业知识之外没有专业知识。
  • 第二次,我注意到年龄是两个日期之间的差异,例如2017 - 1917 = 100。我不知道这是否有足够的帮助。您提到了 2017 年 12 月 31 日的月份和每日日期,因此您的数据尚不清楚。

标签: stata data-analysis data-science


【解决方案1】:

您的数据似乎每天都有。在这种情况下,您需要的是:

gen current_date=date("20171231","YMD")
format current_date %td //this will be the variable from which age will be calculated
gen age=current_date-start_credit_date //again, assuming the start credit variable is daily

这将年龄变量作为天数。如果你想要它作为月数,你需要添加:

gen current_month=mofd(current_date)
format current_month %tm
gen start_month=mofd(start_credit_date)
format start_month %tm
gen age_month=current_month-start_month

【讨论】:

  • 很有帮助。您需要在start_month 前面加上gen
猜你喜欢
  • 2023-04-06
  • 2022-11-18
  • 1970-01-01
  • 2017-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多