Stata连享会 主页 || 视频 || 推文 || 知乎 || Bilibili 站
温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。
New!
lianxh
命令发布了:
随时搜索推文、Stata 资源。安装:
. ssc install lianxh
详情参见帮助文件 (有惊喜):
. help lianxh
连享会新命令:cnssc
,ihelp
,rdbalance
,gitee
,installpkg
⛳ Stata 系列推文:
作者: 方森辉 (南开大学)
邮箱: nkfangsenhui@163.com
目录
在使用 Stata 进行数据处理时,我们往往不仅需要生成新的变量,还需要替换原有变量。对于一些简单的变量替换过程,一般我们采用 Stata 的官方命令 replace
命令。不过,在特定情况下,采用外部命令 ereplace
将事半功倍。
ereplace
命令介绍关于该命令的安装,我们既可以使用 Stata 的官方命令 ssc install
,也可以使用连享会最新推出的 cnssc install
命令。
如果使用 cnssc
的话,大家需要事先安装好该命令。与 ssc install
不同的是,cnssc
安装 packages 时是访问对应的国内镜像,因此安装速度要快上不少。与此同时,使用 cnssc
命令安装 ereplace
命令后,将提示是否下载案例数据集 e_example.dta,默认下载该数据集到 Stata 安装目录下。
Stata 中安装上述命令可使用如下代码:
* 使用ssc安装ereplace命令
ssc inst ereplace //install可以简写为inst
* 安装cnssc命令
ssc inst cnssc
* 使用cnssc安装ereplace命令
cnssc inst ereplace //连享会新命令:cnssc=cn+ssc
checking ereplace consistency and verifying not already installed...
all files already exist and are up to date.
help ereplace
Note: 1 ancillary files found, view get
//点击view get按钮将默认下载该数据集到 Stata 安装目录下
安装完毕后,可以使用 ssc des ereplace
命令查看命令简介、版本和作者信息:
* 使用ssc des查看ereplace命令
. ssc des ereplace
---------------------------------------------------------------------------
package ereplace from http://fmwww.bc.edu/repec/bocode/e
---------------------------------------------------------------------------
TITLE
'EREPLACE': module to extend egen and egenmore to permit replacing
DESCRIPTION/AUTHOR(S)
ereplace extends the popular egen and egenmore (if installed)
modules to permit replacing. ereplace replaces varname of the
optionally specified storage type [type] equal to
fcn(arguments). Depending on fcn(), arguments refers to an
expression, a varlist, a numlist, or an empty string. The
options are similarly function dependent.
KW: data management
KW: egen
KW: replace
Requires: Stata version 6
Distribution-Date: 20200511
Author: Nicholas J. Cox, Durham University
Support: email N.J.Cox@durham.ac.uk
Author: Chris Larkin, The Behavioural Insights Team
Support: email chris.larkin@bi.team
INSTALLATION FILES (type net install ereplace)
ereplace.ado
ereplace.sthlp
ANCILLARY FILES (type net get ereplace)
e_example.dta
----------------------------------------------------------------------------
(type ssc install ereplace to install)
安装好 ereplace
命令后,我们可以输入 help ereplace
,查看 ereplace
的帮助文档。
. ereplace [type] varname = fcn(arguments) [if exp] [in range] [, options]
type
:变量类型,即变量在Stata中的存储格式,涉及数值型变量(byte、int、long、float、double)和文字型变量(str1、str2、…、str2045)两大类varname
:ereplace
命令执行后被替换的变量名称fcn
:运算类型,涉及 count、diff、group、mad、max、mdev、mean、median、min、mode、seq、std、tag、total 等,具体也可参见 help egen
或 help egenmore
arguments
:ereplace命令执行涉及的变量命令if
、in
等条件语句,以及对于部分运算类型(fcn
)可以添加其他选项(option)
以下将介绍几个实例来加深对ereplace
命令的了解。
replace
和ereplace
使用对比首先,我们对比replace
和ereplace
对相同问题的差异化处理,展现其强大之处,如保留文字型变量strvar
中的特定字符串(1、3、7、S、H
)。
global path "C:\Users\FSH\Desktop\Input"
cd "$path" //设置工作目录
replace
、forvalue
、usubstr
* 以下代码局部暂元`max_len'被多处使用,自出现局部暂元`max_len'时需要一次选中运行
. net get ereplace // 下载数据到当前工作路径
. use e_example, clear //导入数据
. des strvar //观察变量strvar的存储格式
. gen len=ulength(strvar) //计算文字变量长度
. sum len
. loc max_len=r(max) //设置拆分字段数量
forv i=1/`max_len'{
//将strvar变量拆分成单个字符形成的变量
gen strvar`i'= usubstr(strvar,`i',1)
}
forv i=1/`max_len'{
//除了特定字符串之外其他字符全部替换为空值
replace strvar`i'="" if strvar`i'!="1" & strvar`i'!="3" & strvar`i'!="7" & strvar`i'!="S" & strvar`i'!="H"
}
replace strvar=""
forv i=1/`max_len'{
//将特定字符串合并为变量strvar,以实现保留特定字符串的目标
replace strvar=strvar+strvar`i'
}
drop strvar1-strvar`max_len' //删除多余的变量
list if _n<=10, clean //展示结果
strvar seqid percent blockid outcome treat len
1. H13 41 13 1 0 0 11
2. 33737 94 86 4 0 0 12
3. H733 26 37 1 0 0 11
4. S71 71 36 3 0 0 11
5. H3717 11 0 1 0 0 12
6. S773 84 99 3 0 0 11
7. 31 48 19 2 0 0 13
8. H177 38 34 1 0 0 9
9. H1 13 65 1 0 0 9
10. 3 52 37 2 0 0 13
ereplace
use e_example, clear //导入数据
ereplace strvar = sieve(strvar), char(137SH) //保留特定字符串
list if _n<=10, clean //展示结果
strvar seqid percent blockid outcome treat
1. H13 41 13 1 0 0
2. 33737 94 86 4 0 0
3. H733 26 37 1 0 0
4. S71 71 36 3 0 0
5. H3717 11 0 1 0 0
6. S773 84 99 3 0 0
7. 31 48 19 2 0 0
8. H177 38 34 1 0 0
9. H1 13 65 1 0 0
10. 3 52 37 2 0 0
接下来,我们将搭配使用replace
、egen
与直接使用ereplace
命令的效果进行对比。
使用到的其他外部命令:egen
。
global path "C:\Users\FSH\Desktop\Input"
cd "$path" //设置工作目录
replace
、egen
use e_example, clear //导入数据集
sort seqid //按照样本ID顺序排列样本
des //观察样本集的指标情况
egen mean_percent = mean(percent) //求percent变量的全样本均值
list seqid mean_percent percent if _n <= 20, clean //显示前20个样本
seqid mean_p~t percent
1. 1 54.81 81
2. 2 54.81 88
3. 3 54.81 3
4. 4 54.81 18
5. 5 54.81 58
6. 6 54.81 41
7. 7 54.81 62
8. 8 54.81 17
9. 9 54.81 36
10. 10 54.81 13
11. 11 54.81 0
12. 12 54.81 26
13. 13 54.81 65
14. 14 54.81 93
15. 15 54.81 82
16. 16 54.81 92
17. 17 54.81 75
18. 18 54.81 52
19. 19 54.81 40
20. 20 54.81 87
drop mean_percent //删除mean_percent变量
//重新生成mean_percent变量,求percent变量按blockid分组的组内均值
egen mean_percent = mean(percent), by(blockid)
sort blockid seqid //确保每次展示数据的样本序一致
by blockid: gen count = _n //按组对样本编号
list seqid mean_percent percent if count <= 5, clean
seqid mean_pe~t percent
1. 1 53.906977 81
2. 2 53.906977 88
3. 3 53.906977 3
4. 4 53.906977 18
5. 5 53.906977 58
44. 44 51.111111 93
45. 45 51.111111 71
46. 46 51.111111 18
47. 47 51.111111 45
48. 48 51.111111 19
62. 62 55.09375 65
63. 63 55.09375 91
64. 64 55.09375 68
65. 65 55.09375 86
66. 66 55.09375 6
94. 94 68.571429 86
95. 95 68.571429 93
96. 96 68.571429 32
97. 97 68.571429 16
98. 98 68.571429 96
ereplace
use e_example, clear //导入数据集
sort seqid //按照样本ID顺序排列样本
des //观察样本集的指标情况
egen mean_percent = mean(percent) //求percent变量的全样本均值
list seqid mean_percent percent if _n <= 20, clean
seqid mean_p~t percent
1. 1 54.81 81
2. 2 54.81 88
3. 3 54.81 3
4. 4 54.81 18
5. 5 54.81 58
6. 6 54.81 41
7. 7 54.81 62
8. 8 54.81 17
9. 9 54.81 36
10. 10 54.81 13
11. 11 54.81 0
12. 12 54.81 26
13. 13 54.81 65
14. 14 54.81 93
15. 15 54.81 82
16. 16 54.81 92
17. 17 54.81 75
18. 18 54.81 52
19. 19 54.81 40
20. 20 54.81 87
//计算按blockid变量分组的组内均值
ereplace mean_percent = mean(percent), by(blockid)
sort blockid seqid //确保每次展示数据的样本序一致
by blockid: gen count = _n //按组对样本编号
list seqid mean_percent percent if count <= 5, clean
seqid mean_pe~t percent
1. 1 53.906977 81
2. 2 53.906977 88
3. 3 53.906977 3
4. 4 53.906977 18
5. 5 53.906977 58
44. 44 51.111111 93
45. 45 51.111111 71
46. 46 51.111111 18
47. 47 51.111111 45
48. 48 51.111111 19
62. 62 55.09375 65
63. 63 55.09375 91
64. 64 55.09375 68
65. 65 55.09375 86
66. 66 55.09375 6
94. 94 68.571429 86
95. 95 68.571429 93
96. 96 68.571429 32
97. 97 68.571429 16
98. 98 68.571429 96
以上就是ereplace
命令的基本介绍,借助两个实例,有助于我们快速便捷的实现多情形下的变量处理工作。
Note:产生如下推文列表的 Stata 命令为:
lianxh 缺失值
安装最新版lianxh
命令:
ssc install lianxh, replace
免费公开课
最新课程-直播课
专题 | 嘉宾 | 直播/回看视频 |
---|---|---|
⭐ 最新专题 | 文本分析、机器学习、效率专题、生存分析等 | |
研究设计 | 连玉君 | 我的特斯拉-实证研究设计,-幻灯片- |
面板模型 | 连玉君 | 动态面板模型,-幻灯片- |
面板模型 | 连玉君 | 直击面板数据模型 [免费公开课,2小时] |
⛳ 课程主页
⛳ 课程主页
关于我们
课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法
等
连享会小程序:扫一扫,看推文,看视频……
扫码加入连享会微信群,提问交流更方便
✏ 连享会-常见问题解答:
✨ https://gitee.com/lianxh/Course/wikis
New!
lianxh
命令发布了:
随时搜索连享会推文、Stata 资源,安装命令如下:
. ssc install lianxh
使用详情参见帮助文件 (有惊喜):
. help lianxh