|
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
Re: st: Invalid file specification
On Jul 7, 2007, at 5:09 AM, Hendri Adriaens wrote:
I'm trying to do something for several files, using a loop. But
somehow, it
doesn't work and the error message "invalid file specification"
doesn't
really help.
#delimit;
forvalues x=1/4 {;
if `x'==1 {local bestand="parsed_cfd.txt"; local zk="cfd";};
if `x'==2 {local bestand="parsed_qds.txt"; local zk="qds";};
if `x'==3 {local bestand="parsed_vfd.txt"; local zk="vfd";};
if `x'==4 {local bestand="parsed_tsa.txt"; local zk="tsa";};
#delimit;
clear;
/* Inlezen */
insheet a b c d e using "`bestand'", tab;
[some more commands]
};
I find that most Stata expressions that appear to require an -if-
statement (as opposed to the -if- qualifier) can be transformed into
something that does not, and it usually works better as well. In
your case, one possibility would be something like (note: untested!):
local zklist "cfd qds vfd tsa"
foreach zk of local zklist {
clear
insheet a b c d e using "parsed_`zk'.txt", tab // Inlezen
[some more commands]
}
Notes:
1. My personal preference is to use the perfectly fine carriage
return delimiter, rather than littering one's code with semicolons;
arguably in your case the use of -#delimit;- made it more difficult
to find your error.
2. You don't say what "[some more commands]" constitutes, but if you
don't need "x" later (which the above code presumes), then it's just
more overhead that (empirically) can lead to potential errors.
3. If you do not need the "bestand" local macro later, then you can
follow the suggested code above. Otherwise, within the loop you can
create a local macro as
local bestand = "parsed_`zk'.txt"
4. It isn't clear to me that your -if- statements are literally
invalid Stata code: they merely did not return the values you
intended ("bestand" appears to have not been defined), which then led
to problems down-stream. But I doubt any error reporting convention
would have been able to identify your specific problem -- and the
suggestions of Austin & Kit on debugging are worth following.
Hope this helps.
-- Mike
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/