.sysuse auto,clear 从内存中调一笔数据
.help sysuse 帮助菜单 help+其他单词 都弹出帮助菜单 .sysuse dir 出来的结果 ↓
.auto.dta census.dta fscstage1.dta network1.dta strepto.dta voter.dta autornd.dta cholesterol.dta gnp96.dta network1a.dta telomerase.dta xrcise4deprsn.dta bcg.dta citytemp.dta
haloperidol.dta nlsw88.dta tsline1.dta xtline1.dta bplong.dta citytemp4.dta lifeexp.dta nlswide1.dta tsline2.dta bpwide.dta educ99gdp.dta lubin97.dta pop2000.dta
uslifeexp.dta cancer.dta fleiss.dta magnes.dta sp500.dta uslifeexp2.dta
.sysuse sp500,clear 查询sp500的相关数据
.clear 清除数据
.edit 建立空白数据表格
.rename var1 code 把var1(原始表格列命名) 重命名为code .label variable val2 “年龄” .label variable val2 “age”
.help rename 重命名的帮助菜单 webuse renamexmpl 从网上下载一些数据 des (describe the data) 表格中的数据分析 renpfix income inc 批量更改数据 .insheet using \通过命令导入文件 .sysuse auto,clear sumarize price.detail .stata的命令结构如下
[bysort:] command [][][][][]
.help tab(tabulate) sysuse census, clear describe
contains data from D:\\ado\\base/c/census.dta
obs: (观测值) 50 1980 Census data by state vars:(变量名) 13 6 Apr 2009 15:43 size: 3,100 (99.9% of memory free)
------------------------------------------------------------------------------------------------------------------------------------ storage display value
variable name type format label variable label
------------------------------------------------------------------------------------------------------------------------------------
state string字符→str14 %-14s State
state2 str2 %-2s Two-letter state abbreviation
region int %-8.0g cenreg Census region pop long .0gc Population poplt5 long .0gc Pop, < 5 year
pop5_17 long .0gc Pop, 5 to 17 years pop18p long .0gc Pop, 18 and older pop65p long .0gc Pop, 65 and older popurban long .0gc Urban population medage float %9.2f Median age
death long .0gc Number of deaths marriage long .0gc Number of marriages --more--
.label(标签)
label define genderlb 1“男” 2“女”定义好标签 genderlb 1 选项为男,2选项为女
label values gender genderlb 将标签赋予gender .drop xxx 删除某变量或观测 help drop Examples Setup
. sysuse census 提取census数据 Describe the data
. describe 查看详情
Drop all variables with names that begin with pop . drop pop* 删除pop开头的变量 Describe the resulting data . describe 查看详情 Drop marriage and divorce
. drop marriage divorce 删除变量marriage divorce Describe the resulting data . describe
Drop any observation for which medage is greater than 32 . drop if medage > 32 删除medage大于32的数据 Drop the first observation for each region
. by region, sort: drop if _n == 1 将region按sort排列再删除代号为1的数据
Drop all but the last observation in each region
. by region: drop if _n != _N 将region中每个代号保存一个
Keep the first 2 observations in the dataset . keep in 1/2 留下二分之一
Describe the resulting data . describe
.help destring .help in
where range is #
#/# #/l f/# Examples
. sysuse auto
. list price in 10 第十个价格 (any command may be substituted for list)
. list price in 10/20 第十到二十个价格 . list price in 20/l 第二十到最后一个价格 l=last (lowercase el at end of range)
. list price in 1/10 第一到十个价格 (numeric 1 at beginning of range)
. list price in f/10 第一到十个价格 f=first (f means the same as 1) . list price in -10/l 倒数第十到最后一个 (lowercase el at end of range)
.help sort Examples Setup
. sysuse auto
. keep make mpg weight 保留mpg weight
arrange observations into ascending order based on the values of mpg . sort mpg 按mpg排列
same as above, but for observations with the same values of mpg, keep them in the same relative order in the sorted data as they had previously
. sort mpg, stable 按mpg排列,(stable)其余的数据按原先后排列 list the 5 cars with the lowest mpg . list make mpg in 1/5
list the 5 cars with the highest mpg . list make mpg in -5/L
arrange observations into ascending order based on the values of mpg, and within each mpg category arrange observations into ascending order based on the values of weight . sort mpg weight
list the 8 cars with the lowest mpg, and within each mpg category with the lowest weight . list in 1/8
..<.a<.b 在数据中空白数据用.表示,排列时剩余的空白数据降序排列 如 2 4 3 .a . .b 排序为2 3 4 . .a .b .help generate, help replace syntax
create new variable 创造新变量
generate [type] newvar[:lblname←标签] =exp [if] [in] gen var3_1=real(var3) 将var3定义成正确的,并且生成var3_1 destring var3=gen (var3_1) 讲
replace contents of existing variable
replace oldvar =exp [if] [in] [, nopromote]
create variable posttran, with storage type of byte, equal to 1 for the second observation of each id and equal to 0 otherwise
. by id: generate byte posttran = (_n==2) 通过id排列,创造一个两个字符的变量posttran
create variable t1 equal to stime for the last observation of id . by id: generate t1 = stime if _n==_N . . .