文件(...).csv";Not Stata文件使用合并命令时出错 [英] file "(...).csv" not Stata file error in using merge command
本文介绍了文件(...).csv";Not 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
文件。
这篇关于文件(...).csv";Not Stata文件使用合并命令时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文