Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: confirm file(s) exist before processing


From   "Trelle Sven" <[email protected]>
To   <[email protected]>
Subject   st: confirm file(s) exist before processing
Date   Thu, 4 Feb 2010 22:34:00 +0100

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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index