Statalist The Stata Listserver


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

st: ordering variable values


From   Kit Baum <[email protected]>
To   [email protected]
Subject   st: ordering variable values
Date   Tue, 9 Jan 2007 22:54:54 -0500

+---------------------------------------------+
| city state year cand1 cand2 cand3 |
|---------------------------------------------|
1. | A TX 2001 Tom Dick Harry |
2. | A TX 2005 Dick Jane Harry |
3. | B MA 2002 John Jim Jack |
4. | B MA 2003 Jim Jill Joan |
5. | B MA 2005 John Jill Jim |
+---------------------------------------------+

. g cs = city+state+string(year)

. reshape long cand,i(cs) j(candnr)
(note: j = 1 2 3)

. bys city cand: g last=(_n==_N)

. egen totcand=total(last),by(city)

. drop last

. reshape wide
(note: j = 1 2 3)

+-----------------------------------------------------------------+
| cs cand1 cand2 cand3 city state year totcand |
|-----------------------------------------------------------------|
1. | ATX2001 Tom Dick Harry A TX 2001 4 |
2. | ATX2005 Dick Jane Harry A TX 2005 4 |
3. | BMA2002 John Jim Jack B MA 2002 5 |
4. | BMA2003 Jim Jill Joan B MA 2003 5 |
5. | BMA2005 John Jill Jim B MA 2005 5 |
+-----------------------------------------------------------------+


Computations like this, as many Statalist participants point out, are usually much simpler in the long format.


Kit Baum, Boston College Economics
http://ideas.repec.org/e/pba1.html
An Introduction to Modern Econometrics Using Stata:
http://www.stata-press.com/books/imeus.html


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




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