英文:
Appending and Looping in Stata - Is it possible to see which file generated the error message
问题
The error message you provided is related to a variable mismatch when using Stata's append
command. It mentions that the variable "company_number" is a string (str101) in the master dataset but a double in the using dataset. To identify which file in the loop caused this error, you can add a line inside the loop to display the current file name. Here's the modified code:
set more off
local files : dir "$analysis/clean_stata" files "*.dta"
foreach file in `files' {
display "Processing file: `file'"
append using "`file'"
}
This modification will print the name of the file currently being processed in the loop, helping you identify which file caused the variable mismatch error.
英文:
This is a general question for Stata's append
command. Is there a way to know which file messed up the append
loop due to a variable mismatch?
For example, if I receive this following error message am I able to tell which file using file the loop is on? Below is the code and error message.
set more off
local files : dir "$analysis/clean_stata" files "*.dta"
foreach file in `files' {
append using `files'
}
Error message:
variable company_number is str101 in master but double in using data
You could specify append's force option to ignore this string/numeric mismatch. The using variable would then be treated as if it contained "".
答案1
得分: 3
I think you could add a display
command:
set more off
local files : dir "$analysis/clean_stata" files "*.dta"
foreach file in `files' {
display "Trying to append `file'" // I added code here!
append using `file' // I also put "file" instead of "files" as I think it's what you meant
}
This should allow you to see which file caused the issue.
英文:
I think you could add a display
command:
set more off
local files : dir "$analysis/clean_stata" files "*.dta"
foreach file in `files' {
display "Trying to append `file'" // I added code here!
append using `file' // I also put "file" instead of "files" as I think it's what you meant
}
This should allow you to see which file caused the issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论