Statalist


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

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


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: confirm file(s) exist before processing
Date   Fri, 5 Feb 2010 11:51:20 -0000

On the face of it, the problem may be a typo.

Where is local macro num ever defined? Is it defined before this block
of code? Is there a confusion between -c- and -num-? 

If that is not the answer, it is difficult to explain your problems
because your code depends on what is upstream and on what your files are
in ways that we can not check. 

What appears to be the common feature is that Stata cannot find files
with names of the forms 

"${bd}\data`anal'_direct`num'.txt"

"${bd}\data`anal'_direct`num'_wo41.txt"

Therefore I suggest trying to -display- the corresponding text. That
will probably reveal some problem with your assumptions. 

Also, 

1. Consider -cd- at the outset, or conversely copying all the data files
into the directory or folder in which you produce results. That
simplifies code, and makes it a bit more efficient. 

2. Beware backslashes -- which even under Windows are not essential. 

SJ-8-3  pr0042  . . . . . . .  Stata tip 65: Beware the backstabbing
backslash
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N.
J. Cox
        Q3/08   SJ 8(3):446--447                                 (no
commands)
        tip to use forward slash instead of backslash for
        directory and file names

Nick 
[email protected] 

Stas Kolenikov

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 <[email protected]>
wrote:

> 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").

> 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