# Stata编程：暂元local和global的使用技巧

Stata连享会   主页 || 视频 || 推文 || 知乎 || Bilibili 站

New！ `lianxh` 命令发布了：

`. ssc install lianxh`

`. help lianxh`

⛳ Stata 系列推文：

• Cox N J. Stata tip 138: Local macros have local scope[J]. The Stata Journal, 2020, 20(2): 499-503. -PDF-
• Herrin J. Stata tip 77:(Re) using macros in multiple do-files[J]. The Stata Journal, 2009, 9(3): 497-498. -PDF-

## 3. 暂元 (macros) 使用技巧

### 3.1 全局暂元 (global macros)

• 直接在命令窗口中输入命令
• 运行整个 do 文件或 do 文件中的某段代码
• 编写的程序 (program)

``````global name "value"
``````

``````. sysuse auto, clear
. * 将表示汽车特征的一组变量放入全局暂元 size 中
. global size " weight length trunk displacement "
. regress price \$size

Source |       SS           df       MS      Number of obs   =        74
-------------+----------------------------------   F(4, 69)        =      9.22
Model |   221276728         4  55319182.1   Prob > F        =    0.0000
Residual |   413788668        69  5996937.22   R-squared       =    0.3484
Total |   635065396        73  8699525.97   Root MSE        =    2448.9
------------------------------------------------------------------------------
price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
weight |      4.649      1.412     3.29   0.002        1.831       7.466
length |   -102.309     43.083    -2.37   0.020     -188.259     -16.360
trunk |     27.899     97.905     0.28   0.777     -167.416     223.215
displacement |      0.616      7.026     0.09   0.930      -13.401      14.633
_cons |  10850.978   4628.016     2.34   0.022     1618.338   20083.619
------------------------------------------------------------------------------
``````

``````. sum price, detail
Price
-------------------------------------------------------------
Percentiles      Smallest
1%         3291           3291
5%         3748           3299
10%         3895           3667       Obs                  74
25%         4195           3748       Sum of wgt.          74

50%       5006.5                      Mean           6165.257
Largest       Std. dev.      2949.496
75%         6342          13466
90%        11385          13594       Variance        8699526
95%        13466          14500       Skewness       1.653434
99%        15906          15906       Kurtosis       4.819188

. * 将中位数的值存储到全局暂元 median_price 中
. global median_price = r(p50)
. display "\$median_price"
5006.5
``````

### 3.1 局部暂元 (local macros)

``````local name "value"
``````

``````. sysuse auto, clear
. local size "weight length trunk displacement" // 定义局部暂元 size
. regress price `size'                          // 回归分析

Source |       SS           df       MS      Number of obs   =        74
-------------+----------------------------------   F(4, 69)        =      9.22
Model |   221276728         4  55319182.1   Prob > F        =    0.0000
Residual |   413788668        69  5996937.22   R-squared       =    0.3484
Total |   635065396        73  8699525.97   Root MSE        =    2448.9
------------------------------------------------------------------------------
price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
weight |      4.649      1.412     3.29   0.002        1.831       7.466
length |   -102.309     43.083    -2.37   0.020     -188.259     -16.360
trunk |     27.899     97.905     0.28   0.777     -167.416     223.215
displacement |      0.616      7.026     0.09   0.930      -13.401      14.633
_cons |  10850.978   4628.016     2.34   0.022     1618.338   20083.619
------------------------------------------------------------------------------
``````

``````. local size "weight length trunk displacement" // 先运行这一行命令
. regress price `size'                          // 再运行这一行命令

Source |       SS           df       MS      Number of obs   =        74
-------------+----------------------------------   F(4, 69)        =      9.22
Model |   221276728         4  55319182.1   Prob > F        =    0.0000
Residual |   413788668        69  5996937.22   R-squared       =    0.3484
Total |   635065396        73  8699525.97   Root MSE        =    2448.9
------------------------------------------------------------------------------
price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
weight |      4.649      1.412     3.29   0.002        1.831       7.466
length |   -102.309     43.083    -2.37   0.020     -188.259     -16.360
trunk |     27.899     97.905     0.28   0.777     -167.416     223.215
displacement |      0.616      7.026     0.09   0.930      -13.401      14.633
_cons |  10850.978   4628.016     2.34   0.022     1618.338   20083.619
------------------------------------------------------------------------------
``````

### 3.3 取长补短的 include 命令

`include` 命令可以实现将指定的 do 文件内容插入命令行位置以取代该命令行，从而将指定的文件和当前的源程序文件连成一个源文件。`include` 命令的调用方法 `include "文件名"`。下面我们仍然运用 Stata 自带的 1978 汽车数据集 auto.dta 来检验此命令。

``````* 生成 locals.do 文件，并保存
local size1 " weight length "
local size2 " trunk displacement "
``````

``````. sysuse auto, clear
. include locals.do

. local size1 " weight length "
. local size2 " trunk displacement "

. regress price `size1' `size2'

Source |       SS           df       MS      Number of obs   =        74
-------------+----------------------------------   F(4, 69)        =      9.22
Model |   221276728         4  55319182.1   Prob > F        =    0.0000
Residual |   413788668        69  5996937.22   R-squared       =    0.3484
Total |   635065396        73  8699525.97   Root MSE        =    2448.9
------------------------------------------------------------------------------
price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
weight |      4.649      1.412     3.29   0.002        1.831       7.466
length |   -102.309     43.083    -2.37   0.020     -188.259     -16.360
trunk |     27.899     97.905     0.28   0.777     -167.416     223.215
displacement |      0.616      7.026     0.09   0.930      -13.401      14.633
_cons |  10850.978   4628.016     2.34   0.022     1618.338   20083.619
------------------------------------------------------------------------------
``````

## 4. 相关推文

Note：产生如下推文列表的 Stata 命令为：
`lianxh 暂元 循环, m`

`ssc install lianxh, replace`

## 相关课程

### 最新课程-直播课

• Note: 部分课程的资料，PPT 等可以前往 连享会-直播课 主页查看，下载。

### 关于我们

• Stata连享会 由中山大学连玉君老师团队创办，定期分享实证分析经验。
• 连享会-主页知乎专栏，700+ 推文，实证分析不再抓狂。直播间 有很多视频课程，可以随时观看。
• 公众号关键词搜索/回复 功能已经上线。大家可以在公众号左下角点击键盘图标，输入简要关键词，以便快速呈现历史推文，获取工具软件和数据下载。常见关键词：`课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法`

✏ 连享会-常见问题解答：
https://gitee.com/lianxh/Course/wikis

New！ `lianxh``songbl` 命令发布了：

`. ssc install lianxh`

`. help lianxh`