Statalist


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

Re: st: drop variable on condition


From   "Joseph Coveney" <jcoveney@bigplanet.com>
To   "Statalist" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: drop variable on condition
Date   Mon, 21 Jul 2008 19:54:28 +0900

Andrea Bennett wrote:

A quick question related to dropping variables: I would like to drop a
variable when a certain condition is met (I'm using loops to check
whether a variable is not empty for each year of observation). AFAIK, -
drop- and -keep- only refer to observations if being used
conditionally, e.g. I cannot use "drop THISVAR if mean_THISVAR<1".

So, how can I drop a variable when a certain condition is met?

--------------------------------------------------------------------------------

You can easily do both.  The do-file below illustrates dropping a variable
(for each year of observations) has any missing values.  And later it drops
it if its mean is greatern than one.

Joseph Coveney

clear *
set seed `=date("2008-07-21", "YMD")'
set obs 200
forvalues i = 1/10 {
   generate double year`i' = uniform()
   if mod(`i', 2) replace year`i' = . if uniform() > 0.99
}
replace year10 = 5 * year10
*
* Begin here
*
foreach var of varlist year* {
   capture assert !missing(`var')
   if _rc {
       display in smcl as text "variable {result}`var' " _continue
       display in smcl as text "contains missing values"
       drop `var'
       continue
   }
   summarize `var', meanonly
   if r(mean) > 1 {
       display in smcl as text "variable {result}`var'" _continue
       display in smcl as text "'s mean is greater than one"
       drop `var'
   }
}
exit


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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