文件"(...).csv"不是 Stata 文件错误在使用合并命令时出错



我使用Stata 12。

我想将文件df_all_cities.csv中的一些国家代码标识符添加到我的工作数据中。

然而,这行代码:

merge 1:1 city country using "df_all_cities.csv", nogen keep(1 3) 

给我一个错误:

. run "/var/folders/jg/k6r503pd64bf15kcf394w5mr0000gn/T//SD44694.000000"
file df_all_cities.csv not Stata format
r(610);

这是一个尝试解决我以前的问题,即该文件是一个dta文件,在此版本的Stata上不起作用,所以我使用R将其转换为.csv,但这也不起作用。我想这是因为命令本身";使用";不适用于csv文件,但我该如何写呢?

你的直觉是对的。命令merge无法直接读取.csv文件。(从技术上讲,using在这里不是一个命令,它是一个常见的语法标记,指示后面的文件路径。(

您需要使用命令insheet读取.csv文件。你可以这样使用它。

* Preserve saves a snapshot of your data which is brought back at "restore"
preserve 

* Read the csv file. clear can safely be used as data is preserved
insheet using "df_all_cities.csv", clear

* Create a tempfile where the data can be saved in .dta format
tempfile country_codes
save `country_codes'
* Bring back into working memory the snapshot saved at "preserve"
restore
* Merge your country codes from the tempfile to the data now back in working memory
merge 1:1 city country using `country_codes', nogen keep(1 3) 

查看insheet如何同时使用using以及此命令如何接受.csv文件。

最新更新