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

st: RE: RE: RE: Referring to a varname, leading to errors


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: RE: Referring to a varname, leading to errors
Date   Wed, 12 Feb 2003 17:40:50 -0000

[email protected]
> 
> I wrote,
> 
> > Abbreviations can hurt you even if you don't ever use them,
> > such as
> > 
> > i)  dropping a variable you never intended to drop; or
> > 
> > ii) inadvertently modifying the wrong variable
> > 
> > For e.g., at some point in time your data contains 
> > variables _foo_ and _foobar_.  You no longer need _foo_
> > so you -drop- it.  Later, (wrongly) believing _foo_ is
> > still defined, you -replace foo = ...- but it modifies
> > -foobar- instead since Stata finds no ambiguity in the 
> > string literal _foo_.  You might never notice this blunder.
> > Dealing with large datasets at times, with hundred of
> > variables possessing names that differ by only a character
> > or two, this can have unfortunate consequences.  I almost
> > never use abbreviations myself either and typically, when
> > I want to modify _foo_, it is _foo_ I want to change and
> > certainly *not* _foobar_.
> 
> to which Nick replied,
> 
> > As mentioned in an earlier posting, safe dropping is 
> > entirely controllable. The question came up on the list a 
> few years 
> > ago and I posted some code, although my guess was then 
> > -- perhaps wrongly -- that it was a one-off request. 
> > 
> >(code omitted, see Nick's earlier email)
> 
> Very true, and a similar routine can be used to handle safe 
> replacing, i.e.
> 
> cap prog drop safereplace
> program def safereplace
> *! in the spirit of safedrop.ado by N.J.Cox
> *! version 1.0   12fev2003   PJoly
> 	version 7.0
> 	
> 	unab V : `1'
> 	if "`V'" != "`1'" {
> 		di as err "`1' incomplete variable name"
> 		exit 198
> 	}
> 	replace `0'
> end

This is safer than -replace-, but note first that 
it misinterprets e.g. 

. safereplace mpg=weight 

as `1' will be "mpg=weight". That's fixable, as
in 

program def safereplace
*! 1.0.1 12 Feb 2003 NJC 
*! version 1.0   12fev2003   PJoly
 	version 7.0
	gettoken v rest : 0, parse("=") 
 	unab V : `v'
 	if "`V'" != "`v'" {
 		di as err "`v' incomplete variable name"
 		exit 198
 	}
 	replace `0'
end

but two issues remain, at least: checking that 
the expression in 

= exp 

does not use unabbreviated varnames and that
any -if- expression does not either. 

> 
> That said, these wrapper routines are not really the answer 
> since any other
> .ado command (whether user-written or official Stata) that 
> drops or replaces
> variables does so using -drop- and not -safedrop-.  Like 
> Lee, I wouldn't go
> as far as picketing in front of Stata headquarters but 
> perhaps I could say
> that a pragmatic module to toggle abbreviations on or off 
> would probably
> help me sleep better at night. :)
> 

Point taken. This illustrates broader comments about the 
wider implications of any change, in that programs you use also 
affect your data: it is more than a matter of your own direct 
input in the Command window and/or .do files. 

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/



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