Stata 日期格式转换

发布时间:2021-10-14 阅读 12503

Stata连享会   主页 || 视频 || 推文 || 知乎 || Bilibili 站

温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。

New! lianxh 命令发布了:
随时搜索推文、Stata 资源。安装:
. ssc install lianxh
详情参见帮助文件 (有惊喜):
. help lianxh
连享会新命令:cnssc, ihelp, rdbalance, gitee, installpkg

课程详情 https://gitee.com/lianxh/Course

课程主页 https://gitee.com/lianxh/Course

⛳ Stata 系列推文:

PDF下载 - 推文合集

作者:左祥太 (武汉纺织大学)
邮箱Shutter_Z@outlook.com


目录


1. 应用场景

当你在进行数据合并时,发现了如下错误:

. merge 1:1 id date using data1.dta
key variable date is float in master but str4 in using data
    Each key variable -- the variables on which observations are matched --
    must be of the same generic type in the master and using datasets.  Same
    generic type means both numeric or both string.
r(106);

又或者在设置面板数据格式时,发现了如下错误:

xtset id date
string variables not allowed in varlist;
date is a string variable
r(109);

相信很多对 Stata 较为陌生的同学都会烦恼于日期格式的处理。为此,本篇推文将为大家简要介绍 Stata 中日期格式转换问题。

2. 格式转换

首先需要确定时间格式,即日度资料、周度资料、月度资料、季度资料、半年度资料还是年度资料;其次就是对号入座使用如下命令:

  • 日度资料
. clear
. input str9 date
          date
  1. "1jan2021"
  2. "2jan2021"
  3. "3jan2021"
  4. "4jan2021"
  5. "5jan2021"
  6. end
. gen date1 = date(date, "DMY") //D代表日, M代表月, Y代表年
. format date1 %td
. gen date2 = date(date, "DMY")
. format date2 %tdCY-N-D 
. des

Contains data
 Observations:             5                  
    Variables:             3                  
------------------------------------------------------------
Variable      Storage   Display    Value
    name         type    format    label      Variable label
------------------------------------------------------------
date            str9    %9s                   
date1           float   %td                   
date2           float   %tdCY-N-D             
------------------------------------------------------------
Sorted by: 
     Note: Dataset has changed since last saved.
  • 周度资料
. input str9 week
          week
  1.  2021w1
  2.  2021w2
  3.  2021w3
  4.  2021w4
  5.  2021w5
  6.  end
. gen week1 = weekly(week, "YW")
. format week1 %tw
. des

Contains data
 Observations:             5                  
    Variables:             2                  
------------------------------------------------------------
Variable      Storage   Display    Value
    name         type    format    label      Variable label
------------------------------------------------------------
week            str9    %9s                   
week1           float   %tw                   
------------------------------------------------------------
Sorted by: 
     Note: Dataset has changed since last saved.
  • 月度资料
. input str6 month
         month
  1. "2021m1"
  2. "2021m2"
  3. "2021m3"
  4. "2021m4"
  5. "2021m5"
  6. end
. gen month1 = monthly(month, "YM")
. format month1 %tm
. gen month2 = monthly(month, "YM")
. format month2 %tmCY-N
. des 

Contains data
 Observations:             5                  
    Variables:             3                  
-------------------------------------------------------------
Variable      Storage   Display    Value
    name         type    format    label      Variable label
-------------------------------------------------------------
month           str6    %9s                   
month1          float   %tm                   
month2          float   %tmCY-N               
-------------------------------------------------------------
Sorted by: 
     Note: Dataset has changed since last saved.
  • 季度资料
. clear
. input str6 quarter
       quarter
  1. "2021q1"
  2. "2021q2"
  3. "2021q3"
  4. "2021q4"
  5. end
. gen quarter1 = quarterly(quarter, "YQ")
. format quarter1 %tq
. des

Contains data
 Observations:             4                  
    Variables:             2                  
-------------------------------------------------------------
Variable      Storage   Display    Value
    name         type    format    label      Variable label
-------------------------------------------------------------
quarter         str6    %9s                   
quarter1        float   %tq                   
-------------------------------------------------------------
Sorted by: 
     Note: Dataset has changed since last saved.
  • 半年度资料
. clear
. input str6 halfyear
      halfyear
  1. "2021h1"
  2. "2021h2"
  3. "2022h1"
  4. "2022h2"
  5. "2023h1"
  6. end
. gen halfyear1 = halfyearly(halfyear, "YH")
. format halfyear1 %th
. des

Contains data
 Observations:             5                  
    Variables:             2                  
-------------------------------------------------------------
Variable      Storage   Display    Value
    name         type    format    label      Variable label
-------------------------------------------------------------
halfyear        str6    %9s                   
halfyear1       float   %th                   
-------------------------------------------------------------
Sorted by: 
     Note: Dataset has changed since last saved.
  • 年度资料
. clear
. input str10 accper
         accper
  1. "2020-12-31"
  2. "2021-12-31"
  3. "2022-12-31"
  4. "2023-12-31"
  5. "2024-12-31"
  6. end
. gen date = date(accper, "YMD")
. format date %tdCY-N-D
. gen year1 = year(date)
. gen year2 = real(substr(accper,1,4))
. gen year3 = yearly(substr(accper,1,4), "Y")
. format year3 %ty
. des

Contains data
 Observations:             5                  
    Variables:             5                  
-------------------------------------------------------------
Variable      Storage   Display    Value
    name         type    format    label      Variable label
-------------------------------------------------------------
accper          str10   %10s                  
date            float   %tdCY-N-D             
year1           float   %9.0g                 
year2           float   %9.0g                 
year3           float   %ty                   
-------------------------------------------------------------
Sorted by: 
     Note: Dataset has changed since last saved.

3. 相关推文

Note:产生如下推文列表的 Stata 命令为:
lianxh 日期 时间, m
安装最新版 lianxh 命令:
ssc install lianxh, replace

相关课程

免费公开课

最新课程-直播课

专题 嘉宾 直播/回看视频
最新专题 文本分析、机器学习、效率专题、生存分析等
研究设计 连玉君 我的特斯拉-实证研究设计-幻灯片-
面板模型 连玉君 动态面板模型-幻灯片-
面板模型 连玉君 直击面板数据模型 [免费公开课,2小时]
  • Note: 部分课程的资料,PPT 等可以前往 连享会-直播课 主页查看,下载。

课程主页

课程主页

关于我们

  • Stata连享会 由中山大学连玉君老师团队创办,定期分享实证分析经验。
  • 连享会-主页知乎专栏,700+ 推文,实证分析不再抓狂。直播间 有很多视频课程,可以随时观看。
  • 公众号关键词搜索/回复 功能已经上线。大家可以在公众号左下角点击键盘图标,输入简要关键词,以便快速呈现历史推文,获取工具软件和数据下载。常见关键词:课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法

连享会小程序:扫一扫,看推文,看视频……

扫码加入连享会微信群,提问交流更方便

✏ 连享会-常见问题解答:
https://gitee.com/lianxh/Course/wikis

New! lianxhsongbl 命令发布了:
随时搜索连享会推文、Stata 资源,安装命令如下:
. ssc install lianxh
使用详情参见帮助文件 (有惊喜):
. help lianxh