Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: invalid syntax - drop


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: invalid syntax - drop
Date   Sun, 29 Oct 2006 15:57:27 -0000

Answers to date support my prejudice that -count- 
is one of the most neglected Stata commands. 

The problem specified is to -drop- variables if
and only if all values are zero or missing. Hence
no values have other values. This then leads
directly to the solution 

foreach v of var cross* { 
	qui count if !(`v' == 0 | missing(`v')) 
	if r(N) == 0 drop `v' 
} 

or, if you prefer, 

foreach v of var cross* { 
	qui count if (`v' == 0 | missing(`v')) 
	if r(N) == _N drop `v' 
} 

If you knew for certain that you had no extended
missings .a ... .z, this could be 

foreach v of var cross* { 
	qui count if !inlist(`v', 0, .) 
	if r(N) == 0 drop `v' 
} 

or, if you prefer, 

foreach v of var cross* { 
	qui count if inlist(`v', 0, .) 
	if r(N) == _N drop `v' 
} 


Note that the -summarize- in other solutions
could be made -, meanonly- with no loss and
some small gain in efficiency. The criterion 
used by Kit that r(max) == 0 & r(sd) == 0 implies 
that all non-missings are 0 could be replaced
by checking r(min) == r(max) == 0.  

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

Kit Baum

> Unfortunately  that doesn't work either.
> If you create a variable that is wholly missing, you will see that r 
> (max) is not defined, so your if condition
> looks like
> 
> if  == 0
> 
> which is surely invalid syntax. You could figure this out if you did
> 
> set trace on
> 
> and then ran your code.
> 
> Given that a variable that is wholly missing and a variable that is  
> always zero have quite different characteristics (for instance, a  
> variable that is zero or missing has r(sum) = 0, but that is not  
> sufficient to conclude that the variable meets your specs) it might be
> easiest to do something like
> 
> foreach v of varlist jun* {
>    qui su `v'
>     if r(N)==0 {
>       drop `v'
>     }
>     if r(sd) == 0 & r(max) == 0 {
>       capture drop `v'
>     }
>   }
> 
 
Phil said
 
> -help drop- shows that the syntax:
> 
>   -drop varlist-
> 
> does not permit an -if- qualifier.
> 
> Perhaps:
> 
> foreach var of varlist cross* {
> qui su `var'
> if `r(max)'==0 {
> drop `var'
> }
> }
 
Socrates Mokkas 

>  > I have a problem with dropping variables. This is my small loop:
>  >
>  > foreach var of varlist cross* {
>  > 	qui su `var'
>  > 	drop `var' if `r(max)'==0
>  > }
>  >
>  > Why is the invalid syntax comming up? What I want to do is 
> to drop  
> variables
>  > ( included in the varlist cross*) if they have values only of  
> zeros or
>  > missing values. Am I missing the obvious here?
 

*
*   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