Calling irregular arguments with syntax anything

The other day I wrote a program that needed to call a file as an argument -- with the full file path. My first pass at it was to capture the argument as usual, with say args input_file. But that would not have worked with file paths that have spaces in them. What might have worked, I guessed, was to use something like this:

syntax namelist
local input_file `namelist'


That, however, choked on a Windows-style file path, because the colon in C:\ is an illegal name. Trial and error (and the [I] book) led me to syntax anything. Here's my scratchpad:

capture prog drop myHello
program myHello

syntax namelist
local names `namelist'
di "Hello `names'"

end

myHello Eenie: Meenie


OK, so "illegal name" it is. Now let's see if "anything" might work better than "namelist":

capture prog drop myHello
program myHello

syntax anything
local names `anything'
di "Hello `names'"

end

myHello c:\my file path here\my file name here.txt


Success. This may look trivial, but everything is after the fact. We have a long-standing policy at the Eno River Analytics worldwide headquarters that we should write toy programs for sketching things out before we go ahead and actually break something.

2 Responses to “Calling irregular arguments with syntax anything

  1. I switched to Vim | A Stata Mind writes:

    [...] This is based on Nick Cox's recipe, slightly altered so you can launch Vim from the Stata command line to edit an existing do-file that say has spaces in it, as in vim "my dofile.do". There's a little more on this "syntax anything" solution in my previous Stata post. [...]

  2. Martin Weiss writes:

    -syntax using- may also be helpful with file paths.

Leave a Reply