第四讲 数据操作和数据管理(一)
参考书目:(Also see)
①Mitchell, M. N. 2010. Data Management Using Stata: A Practical Handbook. College Station, TX: Stata Press.
②Long, J. S. 2009. The Workflow of Data Analysis Using Stata. College Station, TX: Stata Press.
③Hamilton, L. C. 2013. Statistics with Stata: Updated for Version 12. 8th ed. Boston: Brooks/Cole. (中文版见⑤)
④Baum, C. F. 2009. An Introduction to Stata Programming. College Station, TX: Stata Press
⑤《应用STATA做统计分析》(劳伦斯·汉密尔顿著,郭志刚等译,重庆大学出版社,2011)。
⑥《社会统计分析与数据处理技术:STATA软件的应用》(杨菊华,中国人民大学出版社,2008)
数据分析的第一步首先要求将原始数据改造为Stata可用的格式。我们可以将一个新数据通过以下几种方式载入Stata:①键盘输入;②读取原始数据的ASCII格式文件;③将电子表格数据粘贴到数据编辑窗口(Editor)中;④应用第三方数据转换程序将其他电子表格、数据库或统计程序创建的系统数据集直接转换过来。一旦Stata有了内存数据,我们就可以在Stata中将其存为Stata格式,以利将来方便地取用和更新。
数据操作和管理包括如下功能: ⑴基本数据命令 ⑵创建和删除变量 ⑶函数和表达式 ⑷日期和时间
⑸加载,保存,导入和导出数据
1
⑹合并数据 ⑺重塑数据集
⑻标签,显示格式和注释 ⑼变量的更改和重命名 ⑽检查数据 ⑾文件操作 ⑿混合数据命令
当数据管理任务很复杂或需要重复进行时,Stata用户可以编写自己的程序来自动完成这些工作。尽管Stata是因其分析功能而著名,其实它同时也具有广泛的数据管理功能。本讲将介绍其中一些基本功能。
一、基本数据命令
1.[D] use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Load Stata dataset
use Load Stata dataset (打开Stata数据集)
例1:(打开指定变量,可应用条件语句:if /in) use ln_wage grade age tenure race using nlswork describe
2.[D] save . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Save Stata dataset
save Save Stata dataset (保存Stata数据集) save filename (首次保存文件) save, replace (保存且替换已有文件)
3.[D] describe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Describe data in memory or in file
describe Describe data in memory or in file(描述内存或文件中的数据,生成内存数据或文件数据的概要)
注意:第一,变量名保证没有歧义的情况下可以使用缩写,或使用缩写通配符(~)代替省略部分,例如displ~ ;第二,可以指定一系列的变量,如变量1-变量n
2
例1: use states,clear
describe, numbers (变量按顺序编号,变量名前面加上数字序号) describe, fullnames(完整变量名)
describe state region median marriage divorce(变量列表,注意此处用到了缩写) 例2: use census,clear describe pop* (通配符*)
describe state region pop18p(stat或po都是错误的) 例3:
describe using states 例4: use auto, clear
describe (显示报告,内存数据未改变)
describe, replace(没有报告显示,内存数据已改变,生成包含报告显示信息的新数据集) d list
4.[D] edit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Browse or edit data with Data Editor
edit Browse or edit data with Data Editor(利用数据编辑器浏览或修改数据)(可应用条件语句:if和 in)
edit操作汇总:
Modes 三种模式:1.编辑模式; 2.浏览模式;3.过滤模
3
式(包含条件语句:in range;if exp)
The current observation and current variable 了解当前的观测值和当前变量 Assigning value labels to variables 为变量指定值标签 Changing values of existing cells 修改已有值 Adding new variables 增加新变量 Adding new observations 增加新观测值 Copying and pasting 复制和粘贴 Logging changes 工作日志的改变
5.[D] list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . List values of variables
list List values of variables (列表显示变量值。可应用条件语句:if/in;by前缀语句) 例:
use auto,clear list in 1/2
list make mpg weight displ foreign in 46/55, sepby(foreign) (分类显示)
list make mpg weight displ foreign in 46/55, divider sep(2) abbreviate(12) (指定垂直线、水平线和变量名称缩写方式)
list foreign in 51/55, nolabel (显示数字代码而不是标签值)
6.[D] label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Manipulate labels
label Manipulate labels (标签操作) 例1: use auto,clear
4
label data \label variable foreign \
label define repair 1 \label values rep78 repair
tabulate rep78 tabulate rep78,nolabel
numlabel, add (添加前缀数码) tabulate rep78
numlabel repair, remove (删除前缀数码)
numlabel, add mask(\ (修改前缀数码) tabulate rep78 例2: use hbp4,clear describe
label define yesno 0 \ (定义值标签 yesno) label dir (列出全部已定义的值标签的名称) label list(列出全部已定义的值标签的名称和内容)
label define yesno 2 maybe (错误提示)
label define yesno 2 maybe, add (正确,增加标签)
label define yesno 2 maybe, modify (正确,modify可以替代add) label list yesno
label define yesno 2 \错误提示)
label define yesno 2 \ (正确,modify除了增加还有修改值标
5