Roger Harbord <[email protected]> wrote
How do I drop a variable a variable only if it has exactly the name I
specify, rather than any name that begins with the characters I
specify? I
need an way to prevent automatic expansion of variable names. I'm
trying to
avoid falling into the trap that some other programs fall into, such
as
-rndbin- :
. clear
. set obs 10
obs was 0, now 10
. gen xbest=1
. summ
Variable | Obs Mean Std. Dev. Min Max
-------------+-----------------------------------------------------
xbest | 10 1 0 1 1
. rndbin 10 0.2 100
( Generating .......... )
Variable xb created.
. summ
Variable | Obs Mean Std. Dev. Min Max
-------------+-----------------------------------------------------
xb | 10 22 2.828427 16 25
What happened to xbest?? The answer is that -rndbin.ado- contains the
line:
cap drop xb
Which drops any variable name beginning with xb, rather than just
dropping a
variable called exactly xb if one exists.
I'm not criticizing the author of -rndbin- (which has generated
countless
millions of random numbers for me in the last few weeks), as I
actually fell
into this trap myself first and dropped a variable I didn't mean to
from my
dataset, before wondered how other similar programs deal with the
issue. I
happened to look at rndbin because I'm using it a lot at the moment
and it's
a program that adds a variable to your dataset, as mine does.
Obviously the
situation in the example above could be avoided by using first
renaming xbest
to something not beginning with xb before called rndbin, but that's
not the
issue. The user of a program shouldn't have to worry about what their
existing variables are called to avoid the possibility of losing data.
I'd be grateful for advice on what's considered best Stata programming
practice to avoid this pitfall,
>>> In general, if any program is useful to you, but has wired in a
variable name which may exist in your data, it should be worth writing
a copy with, in this case, a -generate()- option which takes your own
supplied new
variable name. Wired-in variable names may fit local conventions
but they can be awkward for general use.
Here is a stab at the problem from another angle:
program def fussydrop
*! NJC 1.0.0 9 August 2002
version 7
syntax varname
args myvar garbage
if "`garbage'" != "" { error 198 }
confirm var `myvar'
if "`varlist'" == "`myvar'" { drop `myvar' }
end
-fussydrop- drops its argument if and only if it is
the full name of a variable. That is,
syntax varname
will expand a varname to its full name (and reject
multiple or ambiguous names).
args myvar garbage
takes what you typed literally. Only if the two are
identical will there be a -drop-.
Nick
[email protected]
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/