Stata Deleting Multiple Observations

1.4k views Asked by At

I have the following data matrix containing ideology scores in a customized dataset:

year    state   cdnum   party   name        dwnom1
1946     23       10    200    WOODRUFF       0.43
1946     23       11    200   BRADLEY  F.    0.534
1946     23       11    200    POTTER  C.    0.278
1946     23       12    200   BENNETT  J.    0.189

My unit of analysis is a given congressional district, in a given year. As one can see state #23, cdnum #11, has two observations in 1946.

What I would like to do is delete the earlier observation, in this case the observation corresponding to name: BRADLEY.F. This happens when a Congressional district has two members in a given Congress. The attempt of code that I have tried is as follows:

 drop if year==[_n+1] & statenum==[_n+1] & cdnum==[_n+1] 

My attempt is a conditional argument, drop the observation if: the year is the same as the next observation, the statenum is the same as the next observation, and the cdnum is the same as the next observation. In this way, I can insure each district has only one corresponding for a given year. When I attempt to run the code I get:

drop if year==[_n-1] & statenum==[_n-1] & cdnum==[_n-1] 
(0 observations deleted)
1

There are 1 answers

0
Nick Cox On

Brief alternative: You should check out the duplicates command.

Detailed explanation of error:

You don't mean what you say to Stata.

Your conditions such as

if year == [_n-1] 

should be

if year == year[_n-1]

and so forth.

[_n-1] 

by itself is treated as if you typed

_n-1 

which is the observation number, minus 1.

Here is a dopey example. Read in the auto data.

. sysuse auto
(1978 Automobile Data)

. list foreign if foreign == [_n-1], nola

     +---------+
    | foreign |
    |---------|
 1. |       0 |
    +---------+

The variable foreign is equal to _n - 1 precisely once, in observation 1 when foreign is 0 and _n is 1.

In short, [_n-1] is not to be interpreted as the previous value (of the variable I just mentioned).

help subscripting gives very basic help.