I cannot even explain this in words:
The above code should result in writing "bar" to a temporary file. Yet, it writes "foo" to the file:
If I leave the by out of the code, I get the expected result.
Also, if I refer to foo[1] before writing to the file, it also produces the correct result.
And, I can produce the correct result with in-line expressions:
I have intentionally omitted a version statement because the same strange behavior occurs in Stata 16 to 18.
What is going on here?
Code:
clear all input str3 foo "foo" "bar" end tempname fh tempfile fn file open `fh' using "`fn'" , write sort foo assert foo[1] == "bar" // <- "bar" is now the first obs. by foo : generate x = 42 file write `fh' (foo[1]) // <- foo[1] == "bar", right? file close `fh' type "`fn'" // <- should be "bar", but we get "foo" instead
Code:
(output omitted) . type "`fn'" // <- but we get "foo" instead foo
Code:
(output omitted) . * by foo : generate x = 42 (output omitted) . type "`fn'" // <- but we get "foo" instead bar
Code:
(output omitted) . by foo : generate x = 42 . display foo[1] bar (output omitted) . type "`fn'" // <- but we get "foo" instead bar
Code:
(output omitted) . file write `fh' "`=foo[1]'" // <- remember that we should get "bar" (output omitted) . type "`fn'" // <- but we get "foo" instead bar
What is going on here?
Comment