Dear all,

First of all, happy new year!

I am writing to ask a question about using a loop with `if', combined with preserve/restore.
The following posts are what I have read so far:

https://www.stata.com/statalist/arch.../msg01117.html
https://www.stata.com/statalist/arch.../msg00105.html
https://www.stata.com/statalist/arch.../msg00484.html
https://www.statalist.org/forums/for...d-saving-files
https://www.stata.com/support/faqs/p...-if-qualifier/

I have a program defined as `match', which requires one argument:

capture program drop match
program define match
keep if ~~~
by var1 : ~~~
end

Using the program above, I want to do a loop using forvalues:

forvalues t=100(1)200{
if t==100 {
preserve
match `t'
save data.dta, replace
restore
}
else {
preserve
match `t'
append using data.dta
save data.dta, replace
restore
}
}

If I run this code, however, I get `data.dta not found' and the loop stops right away.
This loop works only if data.dta is already saved: Thus, I am running the loop after I saved an empty dta file using a command below:
save data.dta, replace emptyok
Although I got it solved, but I want to know why it needs an empty dta file first; should not `replace' option enough to ensure save command to run, if there is no data.dta file?