STATA 学习笔记:导入数据
1. excel 格式的数据导入
(1)打开excel
cd "该excel所在的文件夹路径"
//shell out 工作簿名称.格式
shell out auto.xls
shellout 命令
Description
shellout opens a document from inside Stata without having to specify the exact file path of the program. It also opens an application with or
without specifying a file document to be opened.
(2)导入excel
//import excel 工作簿名称.格式, firstrow sheet(Sheet1) clear
//sheet(Sheet1) 指定选取该工作表中的工作表1
import excel auto.xls, firstrow sheet(Sheet1) clear
//注意:clear前面没有逗号
命令encode
Description
encode creates a new variable named newvar based on the string variable varname, creating, adding to, or just using (as necessary) the value label
newvar or, if specified, name. Do not use encode if varname contains numbers that merely happen to be stored as strings; instead, use generate
newvar = real(varname) or destring; see real() or [D] destring.
webuse hbp2,clear
des sex
encode sex, generate(gender)
//基于字符串变量sex生成新的数值变量gender,此外还添加了值标签,即文字-数字对应
//gender变量的值female对应数字1,male对应数字2
des gender
(3)存储
save myauto.dta,replace
//将转化成stata专用格式的数据存储下来
//数据存储地址即为STATA界面下方显示的当前工作目录
2. STATA数据导出为excel
sysuse auto,clear
export excel auto,firstrow(variables) replace
//export excel 工作簿名称
//firstrow(variables):将变量名称作为excel第一行
shellout auto.xls
sysuse auto,clear
export excel auto1.xlsx,firstrow(varlabels) replace
//将dta格式数据转换成名称为auto1,格式为xlsx的工作簿
//firstrow(varlabels)将变量标签作为工作簿第一行
shellout auto1.xlsx
2. 导入.txt格式数据
import delimited命令
clear
copy http://www.stata.com/examples/auto.csv auto.csv
import delimited auto, colrange(:3) rowrange(8)
//Read the first three columns and last four rows of auto.csv into Stata and list the data
list