Stata连享会 主页 || 视频 || 推文 || 知乎 || Bilibili 站
温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。
New!
lianxh
命令发布了:
随时搜索推文、Stata 资源。安装:
. ssc install lianxh
详情参见帮助文件 (有惊喜):
. help lianxh
连享会新命令:cnssc
,ihelp
,rdbalance
,gitee
,installpkg
⛳ Stata 系列推文:
作者:左祥太 (武汉纺织大学)
邮箱:Shutter_Z@outlook.com
目录
当你在进行数据合并时,发现了如下错误:
. 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 中日期格式转换问题。
首先需要确定时间格式,即日度资料、周度资料、月度资料、季度资料、半年度资料还是年度资料;其次就是对号入座使用如下命令:
. 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.
Note:产生如下推文列表的 Stata 命令为:
lianxh 日期 时间, m
安装最新版lianxh
命令:
ssc install lianxh, replace
免费公开课
最新课程-直播课
专题 | 嘉宾 | 直播/回看视频 |
---|---|---|
⭐ 最新专题 | 文本分析、机器学习、效率专题、生存分析等 | |
研究设计 | 连玉君 | 我的特斯拉-实证研究设计,-幻灯片- |
面板模型 | 连玉君 | 动态面板模型,-幻灯片- |
面板模型 | 连玉君 | 直击面板数据模型 [免费公开课,2小时] |
⛳ 课程主页
⛳ 课程主页
关于我们
课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法
等
连享会小程序:扫一扫,看推文,看视频……
扫码加入连享会微信群,提问交流更方便
✏ 连享会-常见问题解答:
✨ https://gitee.com/lianxh/Course/wikis
New!
lianxh
和songbl
命令发布了:
随时搜索连享会推文、Stata 资源,安装命令如下:
. ssc install lianxh
使用详情参见帮助文件 (有惊喜):
. help lianxh