将年月变量格式化为日期

发布于 2025-01-17 02:56:55 字数 325 浏览 1 评论 0原文

在 Stata 中,我有一个变量 yearmonth ,其格式为 201201、201202 等,表示 2012 - 2019 年,每月一次,没有间隙。当我将变量格式化为

format yearmonth %tm 

所有时期的结果如下所示:2.0e+05,每次都有完全相同的数字。 Dickey-Fuller 测试告诉我,我的数据中有间隙(我没有),并且 tsfill 命令会在每个周期之间生成数十个空观察值。

如何正确设置 yearmonth 变量的格式以便将其设置为每月日期?

In Stata I have a variable yearmonth which is formatted as 201201, 201202 etc. for the years 2012 - 2019, monthly with no gaps. When I format the variable as

format yearmonth %tm 

The results look like: 2.0e+05 for all periods, with the exact same number each time. A Dickey-Fuller test tells me I have gaps in my data (I don't) and a tsfill command generates dozens of empty observations between each period.

How do I properly format my yearmonth variable so I can set it as a monthly date?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁憨憨 2025-01-24 02:56:55

例如,201212 和 201301 之间确实存在差距。考虑这样的语句

gen wanted = ym(floor(yearmonth/100), mod(yearmonth, 100))

,它将像 201201 这样的整数解析为年份和月份部分。所以 floor(201201/100)floor(2012.01) ,所以 2012mod(201201, 100)1。这两个组件是 ym() 的参数,它需要年份和月份参数。

然后,也只有这样,您的 format 语句才会符合您的要求。该命令不会创建日期变量。

有关详细信息,请参阅 Stata 中的帮助日期时间以及在 Stata 17 中将重新格式化的字符串显示为四位数年份的问题,以解释日期值和日期显示格式之间的差异。

You do have gaps — between 201212 and 201301, for example. Consider a statement like

gen wanted = ym(floor(yearmonth/100), mod(yearmonth, 100))

which parses your integers like 201201 into year and month components. So floor(201201/100) is floor(2012.01) and so 2012 while mod(201201, 100) is 1. The two components are then the arguments of ym() which expects a year and a month argument.

Then and only then will your format statement do you want. That command won’t create date variables.

See help datetime in Stata for more information and Problem with displaying reformatted string into a four-digit year in Stata 17 for an explanation of the difference between a date value and a date display format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文