# Re: st: question: how do i...

 From Phil Schumm To statalist@hsphsun2.harvard.edu Subject Re: st: question: how do i... Date Sun, 6 Mar 2005 20:54:45 -0600

At 1:53 AM +0000 3/7/05, israel solaresbravo wrote:
i want to construct a variable called margin of victory:

my data set looks something like this:

1 10 1 6 21
2 11 2 7 22
3 12 3 8 13
4 1 4 2 3
5 2 5 3 7

my question is:

how do i create a variable called margin_of_victory = votes for 1st place - votes for 2nd place?, i.e. 21-10 for state 1, 22 - 11 for state 2, 13 - 12 for state 3, 4 - 3 for state 4, etc.

If you reshape your data from wide to long form, then this is a snap. Here are your data:

. li

+-------------------------------------------+
|-------------------------------------------|
1. | 1 10 1 6 21 |
2. | 2 11 2 7 22 |
3. | 3 12 3 8 13 |
4. | 4 1 4 2 3 |
5. | 5 2 5 3 7 |
+-------------------------------------------+

Now, try the following (output omitted):

. reshape long votes, i(state) j(candidate) string
. bys state (votes): gen margin = votes[_N] - votes[_N-1]
. reshape wide

which produces exactly what you want:

. li

+----------------------------------------------------+
|----------------------------------------------------|
1. | 1 10 1 6 21 11 |
2. | 2 11 2 7 22 11 |
3. | 3 12 3 8 13 1 |
4. | 4 1 4 2 3 1 |
5. | 5 2 5 3 7 2 |
+----------------------------------------------------+

As you can see, the trick is in using -reshape-. If you're not familiar with what -reshape- does, use -list- after the first reshape and take a look, then read [R] reshape. Once the data are in long form, we sort by state and number of votes, and then within each state, compute the difference between the largest number of votes and the next largest (hint: _N refers to the total number of observations within the state, and votes[_N] is the last observation within the state (which, since the observations are sorted by number of votes, is the observation with the largest vote count)).

-- Phil
*
* 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/