Appending and Looping in Stata – 是否可以查看生成错误消息的文件

huangapple go评论49阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年5月11日 04:07:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定