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

st: RE: Deleting variables for lack of observations

From   "Nick Cox" <>
To   <>
Subject   st: RE: Deleting variables for lack of observations
Date   Wed, 26 Feb 2003 18:57:59 -0000

Joel Clovis
> I want to delete variables if they do not contain enough 
> observatoins.  I've
> tried to make use of Nick Coxs'  npresent & dropmiss (STB-49:dm67 &
> STB-60: dm89)  to no avail, some attempts are in the 
> following manner:
> foreach var of varlist cba2tfina-region {
>             keep `var' if r(npresent)>=30
>     }
> this brings up invalid syntax, and I'm not sure why and
> keep if npresent(lifeinsdensity )<30
> results in 0 observations being deleted, even though
> npresent(lifeinsdensity ) without keep..returns  lifeinsdensity  10
> Can anyone help?  I'm a Stata 7 user

1. Use of -npresent-

-npresent- is not a function, so -npresent()- is not 
legal within a command as in your example with -keep-. 

. npresent (lifeinsdensity) 

does work, but this is a gift from Stata's varlist 
syntax, and nothing to do with functions. 

-npresent- does leave behind r(N), but not r(npresent). 
This is a side-effect of its use of -count-. 

-npresent- is really a function to display information. 
I wouldn't use it this way. 

2. How to -drop- according to your criterion

I'd do it this way: 

. foreach v of var cba2tfina-region { 
.  	qui count if !missing(`v') 
.	if r(N) < 30 { 
.		drop `v' 
.	}
. } 

You need to be straight on the difference between (say) 

if r(N) < 30 { 
	drop `v' 


drop `v' if r(N) < 30 

As it happens, the second is not legal syntax, 
but the difference in general is important: 

3. -dropmiss-

As far as I can see, -dropmiss- offers nothing 
for this problem over the first principles approach. 

*   For searches and help try:

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