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)
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
should be
and so forth.
by itself is treated as if you typed
which is the observation number, minus 1.
Here is a dopey example. Read in the auto data.
The variable
foreign
is equal to_n - 1
precisely once, in observation 1 whenforeign
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.