global lst "A B C D"
egen maxscore = rmax(votesA votesB votesC votesD)
foreach i of global lst {
gen diff`i'= maxscore - votes`i'
replace diff`i'=. if diff`i'==0
}
egen margin_of_victory = rmin(diffA diffB diffC diffD)
Anirban
-----Original Message-----
From: Phil Schumm
To: [email protected]
Sent: 3/6/2005 8:54 PM
Subject: Re: st: question: how do i...
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:
>
>
>margin_of_victory>
>
>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
+-------------------------------------------+
| state votesa votesb votesc votesd |
|-------------------------------------------|
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
+----------------------------------------------------+
| state votesa votesb votesc votesd margin |
|----------------------------------------------------|
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/
NOTICE OF CONFIDENTIALITY - This material is intended for the use of the
individual or entity to which it is addressed, and may contain information
that is privileged, confidential and exempt from disclosure under applicable
laws. If the reader of this material is not the intended recipient, you are
hereby notified that any dissemination, distribution or copying of this
communication is strictly prohibited. Please notify the sender of the error
and destroy the Email you received.
*
* 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/