在Stata中转换字符串每月日期 [英] Transform string monthly dates in Stata

查看:3679
本文介绍了在Stata中转换字符串每月日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题在Stata与日期的格式。我相信这是一个非常简单的问题,但我看不到如何解决它。

I have a problem in Stata with the format of the dates. I believe it is a very simple question but I can't see how to fix it.

我有一个csv文件(file.csv),看起来像

I have a csv file (file.csv) that looks like

v1            v2
01/01/2000    1.1
01/02/2000    1.2
01/03/2000    1.3
...    
01/12/2000    1.12
01/02/2001    1.1
...
01/12/2001    1.12

v1的形式是dd / mm / yyyy。

The form of v1 is dd/mm/yyyy.

我使用 import delimited ... file.csv 导入Stata中的文件

I import the file in Stata using import delimited ...file.csv

v1是一个字符串变量,v2是一个float。

v1 is a string variable, v2 is a float.

我想在Stata可以读取的每月日期中转换v1。

I want to transform v1 in a monthly date that Stata can read.

我的尝试次数:

1)

gen Time = date(v1, "DMY")
format Time %tm

这给了我

Time
3177m7
3180m2
3182m7
...

错误。

2)替代

gen v1_1=v1
replace v1_1 = substr(v1_1,4,length(v1_1))
gen Time_1 = date(v1_1, "MY")
format Time_1 %tm


如果我输入

tsset Time, format(%tm)

但数据中没有间隙。

您能帮我了解我做错了什么吗?

Could you help me to understand what I'm doing wrong?

推荐答案

Stata对日期和时间有精彩的文档,如果您计划,您应该从头到尾阅读使用时间相关变量。阅读本文档不仅可以解决您当前的问题,而且可以防止将来出现代价高昂的错误。与您的问题相关的部分标题为SIF到SIF转换。 SIF表示Stata内部形式。

Stata has wonderful documentation on dates and times, which you should read from beginning to end if you plan on using time-related variables. Reading this documentation will not only solve your current problem, but will potentially prevent costly errors in the future. The section related to your question is titled "SIF-to-SIF conversion." SIF means "Stata internal form."

为了解释您当前的问题:

To explain your current issue:

您在分配格式时将它们解释为日期。请考虑以下内容:

Stata stores dates as numbers; you interpret them as "dates" when you assign a format. Consider the following:

set obs 1
gen dt = date("01/01/2003", "DMY")
list dt
// 15706

让我们将其格式化为一天:

So that date is assigned the value 15706. Let's format it to look like a day:

format dt %td
list
// 01jan2003

现在让我们将其格式化为一个月:

Now let's format it to be a month:

format dt %tm
list
// 3268m11

请注意, dt 只是一个数字,您可以格式化和使用像一天或一个月。要从天数中获取月份数,请执行以下操作:

Notice that dt is just a number that you can format and use like a day or month. To get a "month number" from a "day number", do the following:

gen mt = mofd(dt)  // mofd = month of day
format mt %tm
list
//      dt       mt
// 3268m11   2003m1

变量 mt 现在等于516. 2003年1月是从1960年1月起的516个月。Stata的时代时间是1月1,1960 00:00:00.000。日期变量存储为自纪元时间以来的天数,datetime变量存储为自纪元时间以来的毫秒数。一个月份变量可以存储为自从时代起的月份(即%tm 格式化决定了哪个月份显示)。

The variable mt now equals 516. January 2003 is 516 months from January 1960. Stata's "epoch time" is January 1, 1960 00:00:00.000. Date variables are stored as days since the epoch time, and datetime variables are stored as miliseconds since the epoch time. A month variable can be stored as months since the epoch time (that's how the %tm formatting determines which month to show).

这篇关于在Stata中转换字符串每月日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆