Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Date 转换日期变量,格式为;JA11“;至今在斯塔塔_Date_Stata - Fatal编程技术网

Date 转换日期变量,格式为;JA11“;至今在斯塔塔

Date 转换日期变量,格式为;JA11“;至今在斯塔塔,date,stata,Date,Stata,我有一个数据集,它有一个日期变量,现在是两位数字母缩写形式的月份,后面是两位数数字年份,比如2011年1月的JA11。如何将其转换为Stata可以识别的月/年日期 数据结构示例: 。list id1 ins HPR if _ndate()函数可以将几乎任何日期格式转换为经过的日期,这是Stata用来处理日期信息的格式。经过的日期按1960年1月1日起的天数计算。此格式对于添加或减去日期以及更改日期变量的格式非常有用 . tostring datevar, replace format(%20.

我有一个数据集,它有一个日期变量,现在是两位数字母缩写形式的月份,后面是两位数数字年份,比如2011年1月的JA11。如何将其转换为Stata可以识别的月/年日期

数据结构示例:

。list id1 ins HPR if _ndate()函数可以将几乎任何日期格式转换为经过的日期,这是Stata用来处理日期信息的格式。经过的日期按1960年1月1日起的天数计算。此格式对于添加或减去日期以及更改日期变量的格式非常有用

 . tostring datevar, replace format(%20.0f)
 datevar was float now str8

 . replace datevar = "0" + datevar if length(datevar) == 7
 (2 real changes made)

 . list

      +----------+
      |  datevar |
      |----------|
   1. | 12031999 |
   2. | 02081998 |
   3. | 04071997 |
      +----------+

 . gen edatevar = date(datevar,"MDY")

 . format edatevar %td

 . list

      +----------------------+
      |  datevar    edatevar |
      |----------------------|
   1. | 12031999   03dec1999 |
   2. | 02081998   08feb1998 |
   3. | 04071997   07apr1997 |
      +----------------------+
有关更多信息,请访问:

为您的样本数据更新:

gen month=substr(hp,1,2)
gen year=substr(hp,3,4)
replace year="2011" if year=="11" **you can omit this in your real data

您提供的数据不需要此部分。但您可以在实际数据中使用

local yr "03 04 05 06 07 08 09 10 11"
local newyr "2003 2004 2004 2006 2007 2008 2009 2010 2011"
local n: word count `yr'
forvalues i=1/`n'{
local yr`i' : word `i' of `yr'
local newyr`i' : word `i' of `newyr'
bys ids: replace year="`newyr`i''" if year=="`yr`i''"
local i=`i'+1
}


如果“MA”是三月,那么“May”是什么?六月和七月是什么?尼克:没错;这就是为什么我只提供了前3个月的样本数据;我真的应该强调OP应该展示真正的规则。@Metrics:谢谢你的帮助。subtra和replace对我有用。这是关于缩写词的一个很好的观点。我不知道这不仅仅是前两个字母。所以三月是我的妈妈,五月是我的妈妈。六月是JU,七月是JL。在数据结构上,我有个人ID,每个月/年都有一个虚拟指标,用于指示他们在该月/年内是否符合标准(长格式)。我有2003年到2011年的数据,每年每个月都有很多个人ID。但是你给我的帮助很大。多亏了这两种方法。因此,上述方法可行;您可以通过编写循环来缩短代码。如果您提供示例数据,我可以用它更新代码。
local yr "03 04 05 06 07 08 09 10 11"
local newyr "2003 2004 2004 2006 2007 2008 2009 2010 2011"
local n: word count `yr'
forvalues i=1/`n'{
local yr`i' : word `i' of `yr'
local newyr`i' : word `i' of `newyr'
bys ids: replace year="`newyr`i''" if year=="`yr`i''"
local i=`i'+1
}
local mon "JA FE MA AP MY JU JL AU SE OC NO DE"
local newmon "JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC"
local n: word count `mon'

forvalues i=1/`n'{
local mon`i' : word `i' of `mon'
local newmon`i' : word `i' of `newmon'
bys ids: replace month="`newmon`i''" if month=="`mon`i''"
local i=`i'+1
}

egen datevar=concat(month year),punct(" ")
generate datevar1=date(datevar,"MY")
format datevar1 %d


ids hp  month   year    datevar datevar1
1   AP11    APR 2011    APR 2011    01apr2011
1   AU11    AUG 2011    AUG 2011    01aug2011
1   DE11    DEC 2011    DEC 2011    01dec2011
1   FE11    FEB 2011    FEB 2011    01feb2011
1   JA11    JAN 2011    JAN 2011    01jan2011
1   JL11    JUL 2011    JUL 2011    01jul2011
1   JU11    JUN 2011    JUN 2011    01jun2011
1   MA11    MAR 2011    MAR 2011    01mar2011
1   MY11    MAY 2011    MAY 2011    01may2011
1   NO11    NOV 2011    NOV 2011    01nov2011
1   OC11    OCT 2011    OCT 2011    01oct2011
1   SE11    SEP 2011    SEP 2011    01sep2011
2   AP11    APR 2011    APR 2011    01apr2011
2   AU11    AUG 2011    AUG 2011    01aug2011
2   DE11    DEC 2011    DEC 2011    01dec2011
2   FE11    FEB 2011    FEB 2011    01feb2011
2   JA11    JAN 2011    JAN 2011    01jan2011
2   JL11    JUL 2011    JUL 2011    01jul2011
2   JU11    JUN 2011    JUN 2011    01jun2011