Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: question about defining a local
From 
 
Phil Schumm <[email protected]> 
To 
 
[email protected] 
Subject 
 
Re: st: question about defining a local 
Date 
 
Sat, 12 Nov 2011 18:52:27 -0600 
On Nov 12, 2011, at 5:08 PM, Daifeng He wrote:
> This is driving me nuts, but I just cannot see what's going wrong with my simple code.  Basically, I am defining a local to do sample restriction so that I can quicky switch from one sample restriction to another in my regressions.   Can anyboday tell me why my code below is wrong?
> 
> . local state & st=="CA"
> 
> . di "`state'"
> & st==CA"" invalid name
> r(198);
Your macro contains double quotes, so you need to enclose it in compound double quotes (see [U] 12.4 Strings):
    . di `"`state'"'
    & st=="CA"
Note that for what you're doing, you're better off creating an indicator variable, and then using that to restrict your sample (as opposed to trying to store part of a command string in a local macro).  For example:
    mark ca if state & st == "CA"
    reg depvar [indepvars] if ca
The resulting code is more readable, and you're less likely to get bitten by something unexpected WRT macro expansion.
-- Phil
P.S. You may already be aware of this, but note that the statement
    if state
will be true for any non-zero values of state (including missing).
*
*   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/