Question

Suppose I have a file called hello.txt in a directory c:\test\, then the following runs without problems in Stata:

local folder "c:\test"
confirm file "`folder'\hello.txt"

but the following won't

local folder "c:\test"
local file "hello.txt"
confirm file "`folder'\`file'"

How do I combine more than one local into a path like c:\test\hello.txt that can be used in e.g. the confirm file command?

Thanks in advance.

Was it helpful?

Solution

The problem is that \ can act both as escape character and as part of a Windows file path.

When you type something in Stata that contains a local macro, then Stata's first action is to evaluate that macro, i.e. look at its content and replace the macro with that content. Sometimes (rarely, but it can happen) you want to prevent that. That is what an escape character is for.

So what Stata sees in your second example is

c:\test`file'

which is not a valid path.

The easiest solution is to use a / instead of a \, which results in a valid path as far as Stata is concerned and that won't act as an escape character.

For more see: Nicholas J. Cox (2008) Stata tip 65: Beware the backstabbing backslash. The Stata Journal, 8(3): 446--447.

http://www.stata-journal.com/article.html?article=pr0042

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top