Statalist


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

Re: st: confirm file(s) exist before processing


From   Stas Kolenikov <skolenik@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: confirm file(s) exist before processing
Date   Thu, 4 Feb 2010 18:11:46 -0600

You could also check whether the file exists by

local thisfile : dir "." file "${bd}\data`anal'_direct`num'.txt"
if "`thisfile'" == "" {
   * issue an error message
}
else {
   insheet ...
   process ...
   save ...
}

On Thu, Feb 4, 2010 at 3:34 PM, Trelle Sven <strelle@ctu.unibe.ch> wrote:

> 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/
>



-- 
Stas Kolenikov, also found at http://stas.kolenikov.name
Small print: I use this email account for mailing lists only.

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index