Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: drop exact name only

From   Roger Harbord <[email protected]>
To   [email protected]
Subject   st: drop exact name only
Date   Fri, 9 Aug 2002 16:55:39 +0100

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,


*   For searches and help try:

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