Dear all,
I have a data management problem and the approaches I found on the
archive/web do not work.
I have several files that I need to read in (insheet), change some
variables, and save as dta. However, some of the files might not exist
and I do not want my do-file to stop but to just run through. Stata runs
the do-file listed below ones but does nothing (it doesn't even provide
an error message but just lists the do-file and states "end of
do-file").
Any help is appreciated.
Sven
I tried the following three approaches - without success (directory of
the files is in a global):
Approach 1
foreach anal in anal42 anal02 ... {
foreach c of numlist 12 13 23 ... {
tempfile junk
capture copy "${bd}\data`anal'_direct`num'.txt"
`junk', replace
if _rc == 0 {
insheet using
"${bd}\data`anal'_direct`num'.txt", clear
drop if rc=="END"
destring rc, replace force
save "${dd}\`anal'_direct`c'", replace
}
capture copy
"${bd}\data`anal'_direct`num'_wo41.txt" `junk', replace
if _rc == 0 {
insheet using
"${bd}\data`anal'_direct`num'_wo41.txt", clear
drop if rc=="END"
destring rc, replace force
save "${dd}\`anal'_direct`c'_wo41",
replace
}
}
}
Approach 2
foreach anal in anal42 anal02 ... {
foreach c of numlist 12 13 23 ... {
capture confirm file
`"${bd}\data`anal'_direct`num'.txt"' // `"..."' according to help
if _rc == 0 {
insheet using
"${bd}\data`anal'_direct`num'.txt", clear
drop if rc=="END"
destring rc, replace force
save "${dd}\`anal'_direct`c'", replace
}
capture confirm file
`"${bd}\data`anal'_direct`num'_wo41.txt"'
if _rc == 0 {
insheet using
"${bd}\data`anal'_direct`num'_wo41.txt", clear
drop if rc=="END"
destring rc, replace force
save "${dd}\`anal'_direct`c'_wo41",
replace
}
}
}
Approach 3
foreach anal in anal42 anal02 ... {
foreach c of numlist 12 13 23 ... {
capture {
insheet using
"${bd}\data`anal'_direct`num'.txt", clear
drop if rc=="END"
destring rc, replace force
save "${dd}\`anal'_direct`c'", replace
}
capture {
insheet using
"${bd}\data`anal'_direct`num'_wo41.txt", clear
drop if rc=="END"
destring rc, replace force
save "${dd}\`anal'_direct`c'_wo41",
replace
}
}
}
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/