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

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


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: RE: RE: RE: Referring to a varname, leading to errors
Date   Wed, 12 Feb 2003 10:38:56 -0000

Joly.Patrick@ic.gc.ca
 
> 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_.

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. 

Here it is again, modernised: 

!!! beware wrapping by mailer 

================== 
program def safedrop
*! NJC 1.1.0 11 February 2003 	
*! NJC 1.0.0 13 November 2000 	
	version 8.0 
	foreach v of local 0 { 
		unab V : `v' 
		if "`V'" == "`v'" { 
			local OKlist "`OKlist'`v' " 
		} 
		else local badlist "`badlist'`v' " 
	} 
	
	local nb : word count `badlist' 
	if `nb' {
		local names = plural(`nb',"name") 
		di "{p}{txt}incomplete variable `names': {res}`badlist'{p_end}" 
		exit 198 
	} 
	else drop `OKlist' 
end 
======================

and here is an oldstyle program: 

======================
program def safedrop6
* renamed 11 February 2003 
*! NJC 1.0.0 13 November 2000 	
	version 6.0 
	tokenize `0' 
	while "`1'" != "" { 
		unab l1 : `1' 
		if "`l1'" == "`1'" { local OKlist "`OKlist'`1' " } 
		else local badlist "`badlist'`1' " 
		mac shift 
	} 
	tokenize `badlist' 
	if "`1'" != "" { 
		if "`2'" != "" { local s "s" } 
		di in g "`badlist'" in r "incomplete variable name`s'" 
		exit 198 
	} 
	else drop `OKlist' 
end 
=====================

and you don't need a program, as the main idea can 
be made interactive: 

foreach v in myvar myothervar theotherone { 
	unab V : `v' 
	if "`V'" == "`v'" { 
		drop `v' 
	}
	else di "`v' incomplete variable name"
} 

But on the technical issue -- whether it is possible -- 
my instinct is increasingly that it will be difficult, 
or at least to do it in the way that those users who 
want it really would want it to work, bearing in mind 
implications also for programs which you run, knowingly or not. 

However, a technical comment will no doubt come 
in due course from Stata Corp. 

Nick
n.j.cox@durham.ac.uk 

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index