Stata Learning

基本操作

导入数据

If your data are in free format, with variables separated by blanks, commas, or tabs, you can use the infile command.

infile str14 country setting effort change using http://data.princeton.edu/wws509/datasets/effort.raw

基本命令

列举个案 list

list sex-age in 1/10 , nolabel
list sex-age if missing(edu) 

描述数据 describe
计数 count count if foreign==1

计算 display

查看变量 codebook

报告变量的分布 inspect

清理内存 clear

退出 exit

创建路径 mkdir (Make directory)

转向路径 cd (change directory) cd “path” cd .. cd . cd \

显示当前路径 pwd (the path of the current working directory)

显示内容 dir ls sysdir

拷贝文件 copy

删除文件 erase (每次一个文件)

压缩文件 compress

显示内容长度 set scrollbufsize

设置memory 大小 set memory expand 10 duplicates drop

log 文件

捕获日志文件没有关闭的错误:capture log close //关闭所有打开的日志文件

打开一个log文件:log using 文件名

若执行某一指令后的结果没有必要记录下来,则可事先用指令“log off ”暂停记录, 需要记录时再用“log on”继续记录, 最后用“log close”关闭文件。
若已存在 log using result1, replace ;要在其后进行添加,则键入
log using result1, append

只记录命令的、没有命令执行结果输出的:cmdlog using filename [, append replace]


提示:对一次数据处理可以同时打开两类日志文件。前者用于数据分析,后者用于形成do-file文件。

基本操作

查找变量 lookfor
改变变量名 rename old_varname new_varname

生成新变量 generate newvar = exp [if exp][in range]
generate bmi=weight/(height^2)

重新编码 recode recode sex (2=0)(1=1)(else=.),gen(gender)

按某变量排序 sort varlist

排位变量 order order edu2, last

移动变量 move

替换原变量的取值 replace

replace sex=. if sex==9

Continuation Lines

When you are typing on the command window a command can be as long as needed. In a do-file you will probably want to break long commands into lines to improve readability. To indicate to Stata that a command continues on the next line you use ///, which says everything else to the end of the line is a comment and the command itself continues on the next line. For example you could write:

graph twoway (scatter lexp loggnppc) /// (lfit lexp loggnppc) Old
hands might write

graph twoway (scatter lexp loggnppc) /* (lfit lexp loggnppc) */

which “comments out” the end of the line.

在stata中加入时间

复制时间后,
destring date, replace ignore("-")
变成201206格式即可 加入时间那一列的名称叫做TIME
命令tesset : Declare data to be time-series data
tesset TIME 就把数据转换为时间序列的了
gen t= _n 把数据从1开始排序到最后
tsset t
就没有gaps的问题了

回归之后显示结果:
disp e(N)显示数据量