Statalist The Stata Listserver


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

st: invalid syntax - drop


From   Kit Baum <[email protected]>
To   [email protected]
Subject   st: invalid syntax - drop
Date   Sun, 29 Oct 2006 08:59:25 -0500

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'
}
}


Phil

Quoting Socrates Mokkas <[email protected]>:

> Hello All,
>
> 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?
>


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'
}
}



Kit Baum, Boston College Economics
http://ideas.repec.org/e/pba1.html
An Introduction to Modern Econometrics Using Stata:
http://www.stata-press.com/books/imeus.html


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