Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: r(198) error with a foreach loop - problem with '`'


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: r(198) error with a foreach loop - problem with '`'
Date   Thu, 29 Apr 2010 12:25:17 +0100

This is the back-stabbing backslash problem. Stata is confused by the backslash which has two interpretations, as a delimiter and an escape character. Use forward slashes and the problem should go away. In fact only the last need be changed: 

use Work\Datasets\Source/`w'youth,clear

This is documented in the manuals at [U] 18.3.11

18.3.11 Constructing Windows filenames by using macros

Stata uses the \ character to tell its parser not to expand macros. Windows uses the \ character as the directory path separator. Mostly, there is no problem using a \ in a filename. However, if you are writing a program that contains a Windows path in macro path and a filename in fname, do not assemble the final result as `path'\`fname'
because Stata will interpret the \ as an instruction to not expand `fname'. Instead, assemble the final result as `path'/`fname' Stata understands / as a directory separator on all platforms.

And in  

SJ-8-3  pr0042  . . . . . . .  Stata tip 65: Beware the backstabbing backslash
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q3/08   SJ 8(3):446--447                                 (no commands)
        tip to use forward slash instead of backslash for
        directory and file names

Nick 
n.j.cox@durham.ac.uk 

Jamie Fagg

Firstly, thanks to Nick Cox and Austin Nichols for their reply to my
recent question about -esttab-. Both answers provide helpful clues to
where I should be going with the problem, but both also raised some
extra questions which I haven't had a chance to work through yet.

On a separate note, Stata is suddenly telling me that ` is not valid
in my foreach loops and I cannot work out where to look to identify
the problem. Code illustrating the issue is below.

//show that file exists
. use Work\Datasets\Source\dyouth,clear

. des, short

Contains data from Work\Datasets\Source\dyouth.dta
 obs:           773
vars:           101
size:        98,171 (99.9% of memory free)
Sorted by:

//show error with foreach loop
. foreach w in d {
 2.         use Work\Datasets\Source\`w'youth,clear
 3. }
invalid '`'
r(198);

end of do-file

r(198);


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index