[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
RE: st: RE: foreach problem |

Date |
Wed, 17 Sep 2008 13:54:25 +0100 |

To expand a little on Neil's point: Let's flag first that your problem is that Stata didn't understand your -egen- command. Its placing within a -foreach- loop is incidental. When looking at your -egen- command Stata expects an expression as the argument of the -egen- function -mean()-. As it happens, your expression was intended to be a single variable name, but -egen- doesn't know that. (-egen- would never know about any macro references either. When -egen- sees a command line, macro names have already been replaced by contents.) As your expression is a single token, but not a number, the only interpretation possible is that it must be the name of something (a variable, a scalar, etc.) -- but that can't be, because the presence of ' ' makes the token an invalid name. That's good logic, and Stata found your error. It just did not explain it in the way that you expected, but its point of view was totally defensible. It so happens that ' ' within your expression are not illegal, by the way. Your expression could be, in principle, something like stringvar == "'a'" That is unlikely, but it's legal. In wanting Stata to do something more, you raise two very big general questions. First off, Stata can check your syntax, but it is weak on semantics and useless at inferring users' intentions separately from their commands. Second, I guess that almost always when users like you and me type ' ' it is a typo for ` ', but, with some exceptions, Stata's not in the business of issuing warnings, or trying to second guess what you probably mean. The developers could put a lot of effort in trying to trap things that are probably mistakes, but it would be a poor use of effort. On the whole, Stata is built rather on the premise that we are smart enough to see mistakes more quickly the next time we see them. Again, there are exceptions, but I think that's the Stata point of view on this. Nick n.j.cox@durham.ac.uk P.S. In my favourite text editor (Vim) when Stata code is being edited strings `stuff' and 'stuff' are highlighted differently. Neil Shephard Rajesh Tharyan wrote: > Not an excuse for looking carefully enough, but does stata have something > built in to check for this? Yes it's the error message you received stating... > . abnor'var' invalid name > r(198); > It's not very informative as the online help indicates, but it does point out that you need to look at _exactly_ how you've typed your variable names. Although in this particular instance it was your mis-specification of single-quotes around the local macro that was the root cause of the error. See -search r(198)- for more info. * * 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/

**References**:**st: foreach problem***From:*"Rajesh Tharyan" <R.Tharyan@exeter.ac.uk>

**st: RE: foreach problem***From:*"Rajesh Tharyan" <R.Tharyan@exeter.ac.uk>

**Re: st: RE: foreach problem***From:*Neil Shephard <nshephard@nhs.net>

- Prev by Date:
**Re: st: detectable alternative/effect size;** - Next by Date:
**st: RE: Imputing values for an Index** - Previous by thread:
**Re: st: RE: foreach problem** - Next by thread:
**st: Imputing values for an Index** - Index(es):

© Copyright 1996–2015 StataCorp LP | Terms of use | Privacy | Contact us | What's new | Site index |