STATA基本命令

  • 分组输出变量的统计性指标
logout, save(name.sx) word  replace:   ttable3   收益      unit_e_112    unit_e_1111    unit_capital    灌溉*    总播种面积   
性别  年龄   教育分组*   健康   党员   户主打工   ///户主层面 
if  dropid!=1,  by(青贮玉米)  f(%9.3f)    

  • 变量分组注意
     如果采用sort var gen gg=group(var)分组,则按照样本量平均分组,如果里面重复值较多,则每次跑一遍程序则重新打算顺序。
     如果重复值较多,建议采用xtile分组。xtile gg=var, nq(5) ,且xtile默认不会将空缺值分入组内,但是采用group则会分进去。
     可以参见连玉君老师的帖子:
    stata分组注意事项] https://zhuanlan.zhihu.com/p/60729388
  • 输出所有word或者excel的名字目录
gen file=""
cd "*****************\会议论文"
local myfilelist: dir . files "*.docx"
foreach filename of local myfilelist {
 local N=_N+1
    set obs `N'
 replace file=subinstr(`"`filename'"', ".docx", "", .) if _n==`N'
}

local myfilelist: dir . files "*.xls"
foreach filename of local myfilelist {
 local N=_N+1
    set obs `N'
 replace file=subinstr(`"`filename'"', ".xls", "", .) if _n==`N'
}

*stata在输出回归表格时经常出问题,出现如下情况,如何解决呢?


错误信息
- 如果是outreg2输出,则需要转码
outreg2 [ y1 y2  y3 y4  y5 ]  using "D:\OneDrive\Documents****\t2.xls",   stats(coef  tstat)  bdec(4)  tdec(3)   replace    
unicode convertfile D:\OneDrive\Documents****\t2.xls  D:\OneDrive\Documents****\t2.xls, replace srcencoding(UTF-8) dstencoding(UTF-16) 
!D:\OneDrive\Documents****\t2.xls

- 如果是tabout输出,则需要提前建boom文件
file open f using D:\OneDrive\***\bom, write  replace  binary
file write f %1bu (239) %1bu (187) %1bu (191)
tabout cliving  c101 using D:\OneDrive\***\table2.csv,  cells( row)  format(0 ) nnoc  style(csv)  topf(bom) replace  


  • 选择含有某一类的数据变量
gen var=1 if ustrregexm(var1,"山东") & ! (ustrregexm(var2,"浙江$") | ustrregexm(var2,"公司$")) 

  • 导入excel数据
import excel using  xxx.xlsx, cellrange(A2 ) firstrow clear
  • 导入标签
foreach x of varlist *{
local vname=`x'[2]
rename `x' `vname'
}

foreach x of varlist *{
local lname=`x'[1]
label var `x' "`lname'"
}  
  • 调整数据的宽度
    数值型 format x %20.0f 表示左对齐,宽度20个字符
    字符型 format xx %-20s 表示左对齐,宽度20个字符
    *循环变量内的内容
foreach x of p {
tabout  省份 路径,............ sum append
}
  • 循环样本时,提取样本内的内容作为循环用
levelsof 变量, local(p)
  • 循环补空缺值,同时利用while循环,可以结合levelsof来写
local j=1
while `j'<=10{
foreach x of varlist b220-b222 b224 b225-b227  {
sort hhcode pid `x'
bysort hhcode pid :replace `x'=`x'[_n-1] if `x'==.&`x'[_n-1]!=.
}
local j=`j'+1
}
  • stata做多条线性图

![线形图]


image.png
twoway (line  单产 year if  area=="China, mainland" , lcolor(black)  connect(l)   lpattern(solid) lwidth(0.4))  ///
             (line  单产 year if  area=="India", lcolor(black*0.2) lpattern(dash) lwidth(0.4))  ///
             (line  单产 year if  area=="Indonesia" , lcolor(black*0.4)  lpattern(dot) lwidth(0.4))  ///
             (line  单产 year if  area=="Bangladesh", lcolor(black*0.6) lpattern(dash_dot) lwidth(0.4))  ///
             (line  单产 year if  area=="Viet Nam" , lcolor(black*0.8)  lpattern(shortdash) lwidth(0.4))  ///
       ,  xlabel(2010(1)2018.5, labsize(4) labgap(0)  ) ///
        ylabel(400(100)1010, labsize(4) labgap(0))  ///
       xtitle("", size(4) margin(0 0 0 0)) ///
       ytitle("单产(斤/亩)", size(4) margin(0 1.5 0 0)) ///
       graphregion(margin (small))  ///
       xsize(10) ysize(6) ///
       legend(region(lcolor(white)) pos(6) ring(0) col(5) size(4) label(1 "中国") label(2 "印度") label(3 "印尼") label(4 "孟加拉") label(5 "越南")       )   ///
       scheme(s1color) name(p1, replace)
  • 柱状图


    柱状图
graph bar  大米进口 大米出口  , over(年份,  axis(lcolor(black)  ) label(labsize(3) angle(0) labcolor(black))) ///
      ytitle(, size(5) orientation(horizontal) margin(0 -2 0 0)) ///
      ylabel(0(50)410,labsize(6) angle(horizontal) labcolor(black) tlcolor(black) nogrid) ///
      yscale(lcolor(black) lwidth(0.2)) ///
      bargap(5) ///
      plotregion(lcolor(black) lwidth(0.2)) ///
      graphregion(margin(0 5 1 1)) ///
      blabel(total, size(3) format(%9.f)) ///
      bar(1, fcolor(gs10) lcolor(black) lwidth(0.2) lpattern(solid)) ///
      bar(2, fcolor(gs12) lcolor(black) lwidth(0.2) lpattern(shortdash)) ///
      xsize(10) ysize(4) ///
      legend(region(lcolor(white)) pos(11) ring(0) col(2) size(4) label(1 "进口") label(2 "出口")     )   ///
      scheme(s1color)   
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。