Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <n.j.cox@durham.ac.uk> |
To | "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu> |
Subject | st: RE: RE: Re: dropping variables with lots of missing |
Date | Fri, 8 Jul 2011 15:47:43 +0100 |
It's the difference between the -if- command and the -if- qualifier. If I go if <condition> <command> Stata tests <condition> once, and if it's true executes <command> (once). If go <command> if <condition> Stata tests <condition> for each observation and executes <command> for those for which it is true. See also FAQ . . . . . . . . . . . . . . . . . . . . . if command vs. if qualifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . J. Wernow 6/00 I have an if or while command in my program that only seems to evaluate the first observation, what's going on? http://www.stata.com/support/faqs/lang/ifqualifier.html Nick n.j.cox@durham.ac.uk Eric Uslaner Thanks Nick. I am not sure that I fully understand the difference in the two situations, but your solution worked very well Nick Cox What you are doing wrong is misunderstanding -if-. -keep- allows -if- conditions, which it evaluates for each observation in turn. With your syntax, -keep- looks at observation 1 and asks "is it true that r(K_uniq) is 4 or more?" and in your situation that is evidently true when you think about observation 1. Actually knowing observation 1 makes no difference to the question "is r(K_uniq) 4 or more?", but Stata is not concerned about that. You could equally ask ... if 2 == 2 or ... if strpos("Eric Uslaner"", "Eric") for which the answer is true when you think about observation 1. And so on for every observation. Stata is just applying the condition you asked it to apply. Less obliquely put, r(K_uniq) is a property of the entire dataset and can't discriminate between observations or variables. I guess what you want is foreach var of varlist trustbanks-angergovtintervene { misstable sum `var' if r(K_uniq) < 4 drop `var' } -- which is quite different. [...] Eric Uslaner I [...] want to drop variables with lots of missing values. [...] I tried this in a do-file with misstable: foreach var of varlist trustbanks-angergovtintervene { misstable sum `var' keep if r(K_uniq) >= 4 } and it winds up deleting all cases for all variables (only three variables have less than 4 observations). What am I doing wrong? * * 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/