如何在stata中附加子文件夹的所有文件

问题描述 投票:0回答:1

我在两台电脑上工作,一台台式机和一台笔记本电脑, 所以通常我的代码是这样的:

global laptop "laptop/pathToFolder"
global desktop "desktop/pathToFolder"

cd $laptop

* make some awesome calculations and fill two folders out of data files

现在假设我有两个包含 dta 文件的文件夹,我想

append
它们这样我就可以制作一个大数据集。

此代码有效:我从here

frame create coeftab_t
frame change coeftab_t
cd ".\results\coefplots\bonferroni\data\2\"
local theFiles: dir . files "*.dta"
append using `theFiles'

cd $laptop
cd ".\results\coefplots\bonferroni\data\4\"
local theFiles: dir . files "*.dta"
append using `theFiles'
cd $laptop

append
第一个文件夹之后,我想移动到下一个文件夹,所以我必须回到之前的工作目录
$laptop
,然后再次更改。 (我只想在脚本的顶部更改它。)

所以我在聊天 gpt 上浪费了一些时间来尝试这个:

frame create coeftab_t
frame change coeftab_t
local theFiles: dir ".\results\coefplots\bonferroni\data\2\" files "*.dta"
append using `theFiles'
* here the error pops up

local theFiles: dir ".\results\coefplots\bonferroni\data\4\" files "*.dta"
append using `theFiles'

我得到的错误是:

file file_one_fakename.dta not found

但是子文件夹的路径中没有拼写错误或特定文件名。

我怎样才能进行第二次工作?我不想因为我必须使用另一台 PC 而在脚本中多次更改工作目录。

directory append global stata
1个回答
1
投票

您可以根据您使用的计算机将本地设置为不同的内容。由于我无权访问您的文件夹和文件,因此我不知道这是否是您在代码中遇到的问题的确切解决方案。但这回答了我认为你问的一般问题。

* Test what username current computer uses
di "c(username)"

* Set root path depending on what computer you are using
if "c(username)" == "MyLaptop" {
    global path "C:\Users\MyLaptop\myproject\"
}
if "c(username)" == "MyDesktop" {
    global path "C:\Users\MyDesktop\myproject\"
}


cd ".\results\coefplots\bonferroni\data\2\"
local theFiles: dir . files "*.dta"
append using `theFiles'

cd $path
cd ".\results\coefplots\bonferroni\data\4\"
local theFiles: dir . files "*.dta"
append using `theFiles'
© www.soinside.com 2019 - 2024. All rights reserved.