简体   繁体   English

Stata如何在循环中使用'file`导出分隔文件

[英]Stata how to export delimited files using 'file` in a loop

I'm importing dta files in a folder and exporting each to a csv file.我在文件夹中导入 dta 文件并将每个文件导出到 csv 文件。 I'm not sure why but the loop doesn't save the file.我不知道为什么,但循环不保存文件。 Here's the code:这是代码:

global path "file path"
cd "${path}"

* Geocodes for edd
clear
local files: dir "${path}Data\MeasureData\" files "*_edd.dta"

*do loop to bridge file for EDD and 
foreach file in `files' {

    use "${path}Data/MeasureData/`file'", clear
    rename beafips edd_id
    merge m:1 edd_id using Data\TempData\bridge_edd.dta
    keep if _merge==3
    drop _merge
    export delimited "${path}Data\OutgoingData\`file'.csv", replace
    
}

I keep getting error like this:我不断收到这样的错误:

file filepath\Data\OutgoingData.csv saved文件 filepath\Data\OutgoingData.csv 已保存

I was expecting this to be saved as filepath\Data`file'.csv.我期待这被保存为 filepath\Data`file'.csv。 What did I do wrong?我做错了什么?

This is happening because Windows defaults to using backslashes in path directories, but in this context your computer is reading the backslash as an escape character and so isn't interpreting the ` as indicating the beginning of your local files .发生这种情况是因为 Windows 默认在路径目录中使用反斜杠,但在这种情况下,您的计算机将反斜杠读取为转义字符,因此不会将 ` 解释为表示本地files的开头。

This problem won't typically appear on a Mac/Linux machine as they default to using forward slashes in directory paths.这个问题通常不会出现在 Mac/Linux 机器上,因为它们默认在目录路径中使用正斜杠。

So the solution is to change all the \ s to / s in your code.因此解决方案是将代码中的所有\更改为/ See here for a more detailed write-up of the problem:https://journals.sagepub.com/doi/pdf/10.1177/1536867X0800800310有关该问题的更详细说明,请参见此处:https://journals.sagepub.com/doi/pdf/10.1177/1536867X0800800310

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM