常用STATA命令,整理自 《STATA十八讲》

导入模板

rename _all, lower 
foreach v of varlist _all {
	local name = subinstr(`v'[1]+"_"+`v'[2],"_没有单位","",.)
	local name = subinstr("`name'","'","",.)
	label var `v' "`name'"
}
drop in 1/2
recast strL _all 
destring _all, replace 
compress 
... 
local date: display %td_CCYY_NN_DD date(c(current_date), "DMY")
local date = subinstr(trim("`date'"), " " , "-", .)
save 上市公司控制人文件_`date'.dta, replace 

导入大型Excel为文件

set excelxlsxlargefile on

标签 -> 变量名

rename * *_o
foreach var of varlist _all {
	local label : variable label `var'
	local new_name = strtoname("`label'")
	rename `var' `var'_o
	rename `var'_o `new_name'
}

删除多余字符

replace name = subinstr(name,"(","",.) 
replace name = subinstr(name,")","",.) 
replace name = subinstr(name,`"""',"",.) 
replace name = subinstr(name,"-","",.) 
replace name = subinstr(name,".","",.) 
replace name = subinstr(name,"*","",.) 
replace name = subinstr(name,"(","",.) 
replace name = subinstr(name,")","",.) 
replace name = subinstr(name,"、","",.)

Winsor2 所有数值变量

// 备份原有L_变量
rename L_* Original_=

foreach var of varlist stkcd accper Total_Current_Assets  {
	sum `var' 
	if r(N) != 0 { 
		local num_var = "`num_var'" + " `var'" +" L_" + "`var'"
		}
		else {
		di "`var'" //local num_var = "`num_var'" + ""	
	}
	gen L_`var' = L.`var' 
} 
local num_var = subinstr("`num_var'","L_stkcd", "",.) 
local num_var = subinstr("`num_var'","stkcd", "",.) 

批量导入文件夹下文件

clear 
cd d:\
local flist: dir . files "*"  
//其中文件可以通过 local localname: dir dirname files "*" 存放在宏 localname中  
//而文件夹则可以通过 local localname: dir dirname dirs "*"   
foreach f of local flist {  
   ls `f' 
   clear 
   import delimited `f', delimiter("##", asstring) encoding(gb18030)
   save `f'.dta, replace 
}  


filesearch *.xls$, loca(fileList)
foreach file in `fileList' {
	di "`file'" 
	}

批量导入文件夹下子文件夹里的xls文件,并合并最终dta

cd "testDir"
local dirNameList : dir . dirs "*"   
foreach dirName of local dirNameList {
    local fileNameList: dir "`dirName'" files "*.xls"  
    foreach fileName of local fileNameList {  
		clear 
		import excel "`dirName'\\`fileName'", firstrow allstring
		rename _all, lower 
		foreach var of varlist _all {
    		local name = subinstr(`var'[1]+"_"+`var'[2],"_没有单位","",.)
    		local name = subinstr("`name'","'","",.)
    		label var `var' "`name'"
		}
		drop in 1/2
		save `dirName'_`fileName'.dta, replace 
    }
}

local dtaNameList: dir . files "*.dta"  
foreach dtaName of local dtaNameList {  
   append using `dtaName'
   erase `dtaName'
}

删除字符串中的 Tab 键

gen a = regexr(stkcd_str,"`=char(9)'","")

删除非A股

drop if (code>=200000&code<300000)|code>=700000

时间戳转换

clear 
set obs 1
gen double a = 889459200000
gen double a_time = a + tc(1jan1970 08:00)
gen date = dofc(a_time) 
format date %tdCCYY-NN-DD
format a_time %tcCCYY-NN-DD_HH:MM:SS.sss
list 
-------------
clear 
set more off 
cd c:\Downloads\Compressed\BaseEnt\Ori
local flist: dir "." files "*"  
foreach f of local flist {  
	clear 
	ls `f' 
	import delimited `f', delimiter("##", asstring) encoding("utf-8")
	replace v7 = v7 + tc(1jan1970 08:00)
	replace v8 = v8 + tc(1jan1970 08:00)
	format v7 v8 %tcCCYY-NN-DD_HH:MM:SS.sss 
	save ..\\`f'.dta, replace 
}  

判断变量类型

采用如下命令获取:

local type: type varname

use 0`i'年企业价值链位置.dta, clear 
local type: type exp_or_imp 
if !strpos("`type'","str") {
tostring exp_or_imp, force replace 
replace exp_or_imp = "进口" if exp_or_imp  == "1"
replace exp_or_imp = "出口" if exp_or_imp  == "0"
save 0`i'年企业价值链位置.dta, replace 

判断文件

如果不存在相应文件,则生成文件。如果存在,则进行操作

capture confirm file `tempFile'
if _rc {
  tempfile tempFile
  save `tempFile', replace
}
else {
  merge 1:1 a b using `tempFile', nogen
  save `tempFile', replace
}