【问题标题】:Convert date variable, in the form "JA11" to date in Stata将日期变量以“JA11”的形式转换为Stata中的日期
【发布时间】:2013-09-27 19:09:39
【问题描述】:

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

数据结构示例:

。如果 _n 列出 id1 ins HPR

    +--------------------+
    | id1    ins     HPR |
    |--------------------|
 1. |   1   AP11   1 YES |
 2. |   1   AU11   1 YES |
 3. |   1   DE11   1 YES |
 4. |   1   FE11   1 YES |
 5. |   1   JA11   1 YES |
    |--------------------|
 6. |   1   JL11   1 YES |
 7. |   1   JU11   1 YES |
 8. |   1   MA11   1 YES |
 9. |   1   MY11   1 YES |
10. |   1   NO11   1 YES |
    |--------------------|
11. |   1   OC11   1 YES |
12. |   1   SE11   1 YES |
13. |   2   AP11    2 NO |
14. |   2   AU11    2 NO |
15. |   2   DE11    2 NO |
    |--------------------|
16. |   2   FE11    2 NO |
17. |   2   JA11    2 NO |
18. |   2   JL11    2 NO |
19. |   2   JU11    2 NO |
    +--------------------+

.

感谢您的帮助。

【问题讨论】:

    标签: date stata


    【解决方案1】:

    date() 函数几乎可以将任何日期格式转换为经过日期,这是 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 |
          +----------------------+
    

    欲了解更多信息,请访问: http://www.stata.com/support/faqs/data-management/handling-date-information/

    【讨论】:

      【解决方案2】:

      为您的示例数据更新:

      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
      }
      

      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
      

      【讨论】:

      • 如果“MA”是三月,那么五月是什么?六月和七月是什么时候?
      • @尼克:没错;这就是为什么我只提供前 3 个月的样本数据。
      • 相当;我真的应该强调 OP 应该显示真正的规则。
      • @Metrics:感谢您的帮助。减去然后替换对我有用。这是一个关于缩写的好点。我没有意识到这不仅仅是前两个字母。所以三月是马,五月是我的。六月是 JU,七月是 JL。在数据结构上,我有个人 ID,并且每个月/年都有一个虚拟指标,用于表明他们在该月/年是否符合标准(长格式)。我有 2003 年到 2011 年的数据,每年每个月都有很多人 ID。但是你给我的帮助会很大。感谢两者。
      • 所以,上述方法有效;您可以通过编写循环来缩短代码。如果您提供示例数据,我可以用它更新我的代码。
      猜你喜欢
      • 2021-05-21
      • 1970-01-01
      • 2014-05-19
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      • 2019-03-14
      • 1970-01-01
      相关资源
      最近更新 更多