前情回顾[统计软件与数据分析] 如何用stata做时间加减yangyuzhou 发表于 2014-8-16 20:06:53 题目很简单:已知某个日期:2008-08-9,求出90天前的日期和90天后的日期。本来以为很简单,结果怎么也得不到正确答案。PS:以上日期是随便举的,原理上应该是任意一个日期YYYY-MM-DD,都能求出对应的X天前和X天后对应的日期。如果不能用stata做出来,其他相应的软件也行,但需要能对批量日期进行这种处理 本文对此进行讨论。 示例代码clear all
input strL d
"2021-06-04"
"2022-12-31"
"2023-08-01"
end
g t = date(d, "YMD")
g t_3 = t + 3
g temp1 = string(t_3, "%td")
g temp2 = date(temp1, "DMY")
g d_3 = string(year(temp2)) + "-" + ///
string(month(temp2), "%02.0f") + "-" + ///
string(day(temp2), "%02.0f")
l
d得到结果 +-------------------------------------------------------------+
| d t t_3 temp1 temp2 d_3 |
|-------------------------------------------------------------|
1. | 2021-06-04 22435 22438 07jun2021 22438 2021-06-07 |
2. | 2022-12-31 23010 23013 03jan2023 23013 2023-01-03 |
3. | 2023-08-01 23223 23226 04aug2023 23226 2023-08-04 |
+-------------------------------------------------------------+
Contains data
Observations: 3
Variables: 6
--------------------------------------------------------------------------------------
Variable Storage Display Value
name type format label Variable label
--------------------------------------------------------------------------------------
d strL %9s
t float %9.0g
t_3 float %9.0g
temp1 str9 %9s
temp2 float %9.0g
d_3 str10 %10s
--------------------------------------------------------------------------------------
Sorted by: (完)
|