Statalist The Stata Listserver

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

st: invalid syntax - drop

From   Kit Baum <>
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.


foreach var of varlist cross* {
qui su `var'
if `r(max)'==0 {
drop `var'


Quoting Socrates Mokkas <>:

> 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
An Introduction to Modern Econometrics Using Stata:

* For searches and help try:

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