B2b. profile 文档:启动时自动执行的命令
Stata 启动时,会到安装目录下查找名为 profile.do
的文档,若有则自动执行该文档中的所有命令 (即,do profile.do
)。因此,你可以把一些常规的设定都放在 profile.do
中,比如,plus 和 personal 文件夹的默认路径,默认工作路径,回归结果的显示格式等。
1. 创建和修改 profile.do
Step 1: 打开/新建 profile.do 文档。执行如下命令:
"`c(sysdir_stata)'profile.do" doedit
- 如果你此前没有创建过 profile.do 文档,则上述命令执行后会自动打开一个空白的 dofile。可以参照 Step 2 和 Step 3 的介绍添加设置。
c(sysdir_stata)
是 Stata 系统自建的暂元,用于存储 Stata 的安装路径的名称。- 你可以使用
sysdir
命令来列示 Stata 的系统路径,其中列出的第一个路径就是 Stata 的安装路径,如
STATA: D:/stata17/ 因此,c(sysdir_stata)
中存储的内容是D:/stata17/
。
- 你可以使用
Step 2: 修改 → 保存。如下是一个最基本的 profile.do:
do ------------ Sample A--
*----------------------- profile.
*-文件路径设定:外部命令的存放位置global path "D:/stata" // 个人路径,酌情修改
sysdir set PLUS `"$path/plus"' // 外部命令的存放位置
sysdir set PERSONAL `"$path/personal"' // 个人文件夹位置
*----------------------- profile.do ------------ Sample A--
此处使用 sysdir set
命令 ([P] sysdir) 指定了 plus 和 personal 文件夹的位置。需要说明的是:
- 你需要根据你的文件夹设定情况,酌情修改
global path "D:/stata"
这一行中的路径名称 ,并在该路径下预先新建名为 plus 和 personal 的文件夹。 sysdir set ……
后的 PLUS 和 PERSONAL 要大写。
Step 3: 更多设定 (可选)。如下是我正在使用的 profile.do 文档,你可以根据自己的需要酌情修改。注意:更新后的 profile.do 在 重启 Stata 时才会生效。
do ------------ Sample B--
*----------------------- profile.
*-基本设定set update_query off // 不要自动更新
set more off, perma // 不显示 -more- 分页提示信息
help set
*-文件路径设定:外部命令的存放位置, 参见 global path "D:/stata" // 统一存放地址
sysdir set PLUS `"$path/plus"' // 外部命令的存放位置
sysdir set PERSONAL `"$path/personal"' // 个人文件夹位置
cd `"$path/personal"' // 默认工作路径
*-结果显示格式
set cformat %4.3f //回归结果中系数的显示格式
set pformat %4.3f //回归结果中 p 值的显示格式
set sformat %4.2f //回归结果中 se值的显示格式
*-log 文件:自动以当前日期为名存放于 stata/do 文件夹下
* 若 D:/stata/ 下没有 do 文件夹,则本程序会自动建立一个
cap cd `"$path/do"'
if _rc{
mkdir `"$path/do"' //检测后发现无 do 文件夹,则自行建立一个
}
cap log close
cap cmdlog close
local fn = subinstr("`c(current_time)'",":","-",2)
local fn1 = subinstr("`c(current_date)'"," ","",3)
log using $path/do/log-`fn1'-`fn'.log, text replace
cmdlog using $path/do/cmd-`fn1'-`fn'.log, replace
*----------------------- profile.do ------------ Sample B--
我在码云仓库中放置了两份 profile 文档:
如果想在 profile.do
中添加更多的设置,可以输入 set
命令查看 Stata 的系统参数设定 ([R] set),也可以输入 cret list
查看你电脑中当前的参数设定。
Step 4: 查验。重启 Stata,结果窗口应该显示如下信息:
do ... running Stata-Path\profile.
其中,Stata-Path 表示 Stata 的安装路径,可以输入 sysdir
查看。我电脑中显示的结果为:
sysdir
. STATA: D:\stata17\ // Stata-Path (Stata 安装路径)
BASE: D:\stata17\ado\base\
SITE: D:\stata17\ado\site\
PLUS: D:/stata/plus\
PERSONAL: D:/stata/personal\
你还可以尝试一下外部命令是否会安装到 PLUS 文件夹中:在命令窗口中输入 ssc install jwdid, replace
,安装外部命令 jwdid
,结果窗口会显示信息如下:
ssc install jwdid
.
not already installed...
checking jwdid consistency and verifying
installing into D:/stata/plus\... installation complete.
可以看到,jwdid
的相关程序文件被正确安装到 Step 2 中设定的 plus 文件夹中。
2. 临时更改外部命令的下载路径
你可以临时更改外部命令的安装路径 (如,为某个项目下载所依赖的外部命令,以实现版本控制的目的),命令为:
net set ado dirname1
(存放 .ado 和 .hlp 文件的路径);
net set other dirname2
(存放作者提供的数据文件和范例 dofiles 等附件文件)。
其中,dirname
表示你存放外部命令的文件夹的路径名称 (请确保该文件夹实际存在),比如
set ado "F:/paper/Wang_AER/adofiles"
net set ado "F:/paper/Wang_AER/appendix" net
当你重新启动 Stata 时,上述设定会自动失效。因为,每次重启后,Stata 会按照 profile.do 中的设定设置初始参数。因此,若希望在一段时间内保持上述设定,可以将它们写入 profile.do 文档,并将此前的设定临时注释掉 (在行首添加 *
或 //
等注释符号),例如:
do ------------ Sample C--
*----------------------- profile.
*-基本设定
……
help set
*-文件路径设定:外部命令的存放位置, 参见 global path "D:/stata" // 统一存放地址
//sysdir set PLUS `"$path/plus"' // 外部命令的存放位置
//sysdir set PERSONAL `"$path/personal"' // 个人文件夹位置
set ado "F:/paper/Wang_AER/adofiles"
net set ado "F:/paper/Wang_AER/appendix"
net
……
do ------------ Sample C-- *----------------------- profile.
若想在不重启 Stata 的情况下恢复此前的设定,可以执行一遍 profile.do 即可:
do "`c(sysdir_stata)'profile.do"
或者执行如下命令:
set ado "`c(sysdir_plus)'" net
3. 参考资料
- 连玉君, 2020, 聊聊Stata中的profile文件
- Stata profile.do: nice tips
- The profile and sysprofile Do-Files
- Wernow, J.B., StataCorp, How can I automatically execute certain commands every time I start Stata?
- Statalist, 2017, profile.do - useful startups?
- STATA ON A MAC: Setting up a profile.do file,针对苹果用户。