赞
踩
问题:Stata导入csv时报错Note: Unmatched quote while processing row,错误识别为两行数据
使用命令,向Stata中导入csv文件:
import delimited using "D:\DATA\data.csv"
出现很多条目有上述报错,全文类似于:
- Note: Unmatched quote while processing row 1040762; this can be due to a
- formatting problem in the file or because a quoted data element spans
- multiple lines. You should carefully inspect your data after importing.
- Consider using option bindquote(strict) if quoted data spans multiple lines
- or option bindquote(nobind) if quotes are not used for binding data.
看内存里的数据,发现这几条的内容分别被识别到第一个单元格里,其他列都是缺失值,而且它下面还有一整行缺失值
检查原csv文件发现,在Excel软件里看起来正常的文本,其实粘贴到txt里能看到带有双引号,例如:
"文字,文字"——Stata识别错误的条目
文字,文字——Stata识别正确的条目
从警告提示里可知,原数据里的双引号有问题,导致Stata不能正确识别换行的数据
根据我的数据情况,改为以下命令则可以正确识别:
import delimited using "D:\DATA\data.csv", bindquotes(strict) varnames(1)
bindquotes(strict)参数解决双引号识别有误的情况,要求Stata在多行里搜索成对的双引号(默认是loose,只会在单行文本里搜索成对的双引号,导致报错,并且由于找不到结束的第二个双引号,就把原数据识别为两行)
varnames(1)参数是为了令csv第一行读取为变量名。本来import delimited using这个命令本身就默认把第一行作为变量名,没有firstrow这个参数,但是如果加了bindquotes(strict)参数,就会把原文件第一行读取为数据第一行,必须再补一个varnames(1)参数来读取变量名
————补充说明————
“要求Stata在多行里搜索成对的双引号”——如果原数据里双引号囊括的行数太多了,可能会报错:
Unmatched quote exceeded 20 lines while processing row 3341; there may be a problem with your data or perhaps you have a quoted string with too many lines. You may specify maxquotedrows() to override the default behavior.
需要在上述命令里增加一个参数,maxquotedrows(允许检索的最大行数数值)
这个参数只有在搭配bindquote(strict)时才有意义,缺省参数时自动视为maxquotedrows(20),所以超过20行会报错
可以添加maxquotedrows(0)或者maxquotedrows(unlimited),这两个写法是相同意思,即行数不限
import delimited using "D:\DATA\data.csv", bindquotes(strict) varnames(1) maxquotedrows(unlimited)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。