B7. Stata 中的函数功能

整理: 谭睿鹏 (南京大学)
邮箱:

Stata 提供了非常丰富的函数功能,可以大幅提高我们的工作效率。输入 help functions 即可查看 Stata 的函数列表。亦可输入 ihelp functions 在线查看 PDF 手册文档 ([FN] functions)。

本节挑选一些常用的函数,以便说明 Stata 调用函数的基本语法规则,更详细的介绍参见:

B7.1 数学运算函数 (Math functions)

取自然对数或以 10 为底的对数:

sysuse nlsw88.dta,clear
gen ln_wage = ln(wage)
gen log_wage = log10(wage)

对小数取整或四舍五入:

gen int_tenure = int(tenure)
gen round_tenure = round(tenure)

对类别变量可以直接分析或将其转成虚拟变量

des2 industry
gen dummy = inlist(industry,7,8,11)

industry7811 的,dummy = 1,否则 dummy = 0

B7.2 处理字符串的函数 (String functions)

例 1:年-月-日的分离

. use "tostring2.dta", clear

. tostring date_pub,gen(date1)
. gen year  = substr(date1,1,4)
. gen month = substr(date1,5,2)
. gen day   = substr(date1,7,2)
. destring year month day, replace

     +------------------------------------------------+
     | date_pub      date1   div   year   month   day |
     |------------------------------------------------|
  1. | 20070930   20070930    .5   2007       9    30 |
  2. | 20080823   20080823    .3   2008       8    23 |
  3. | 20090102   20090102    .7   2009       1     2 |
  4. | 20081130   20081130    .3   2008      11    30 |
  5. | 20061204   20061204    .4   2006      12     4 |
     +------------------------------------------------+

例 2:银企关系数据中银行名称的提取

想把总行的名称取出来,如从「中国农业银行深圳光明支行」中取出「中国农业银行」:

. use "bankname.dta", clear
. compress

. gen bank = objnm
. replace bank = "中国农业银行" if strmatch(bank,"*农业银行*")
. replace bank = "招商银行"     if strmatch(bank,"*招商*")
. replace bank = "中国银行"     if strmatch(bank,"*中国银行*")
. replace bank = "中国工商银行" if strmatch(bank,"*工商*")
. replace bank = "兴业银行"     if strmatch(bank,"*兴业*")
. replace bank = "光大银行"     if strmatch(bank,"*光大*")
. replace bank = "交通银行"     if strmatch(bank,"*交通*")
. replace bank = "北京银行"     if strmatch(bank,"*北京*")

. list bank objnm in 1/10

     ----------------------------------------------
              bank                           objnm 
     ----------------------------------------------
  1.  中国农业银行            中国农业银行深圳光明支行
  2.      招商银行        招商银行股份有限公司兰州分行
  3.      光大银行               光大银行深圳罗湖支行
  4.  中国工商银行     中国工商银行股份有限公司萍乡分行
  5.      中国银行                   中国银行吉首支行
     ----------------------------------------------
  6.      中国银行      中国银行股份有限公司深圳市分行
  7.      北京银行                   北京银行上海分行
  8.      中国银行                          中国银行
  9.      兴业银行               兴业银行深圳科技支行
 10.  中国农业银行             中国农业银行大连市分行
     ----------------------------------------------

写命令本身并不复杂,但需要对数据进行仔细查看,总结规律。

其他字符函数

help substr()     //截取
help strmatch()   //查找替换
help strpos()     //字符出现的位置
help strinstr()   //替换字符
help strinword()  //替换单词

B7.3 生成随机数的函数 (Random-number functions)

在蒙特卡洛模拟分析中,我们需要生成服从特定分布的随机数,Stata 提供了多数常见随机数发生器。

clear
set obs 10000

gen x_u = runiform()  // 服从 U~(0,1)均匀分布的随机数
gen x_n = rnormal()   // 服从标准正态分布的随机数
gen x_p = rpossion(5) // 服从自由度为5的泊松分布

进一步可用 simulate 命令做蒙特卡洛模拟或用 bootstrap 命令做 bootstrap。

help simulate
help bootstrap

相关推文

Stata 命令:. lianxh 抽样 蒙特 随机数 函数, m