Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: -reshape-, or -by-?


From   "Nick Winter" <nwinter@policystudies.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: -reshape-, or -by-?
Date   Wed, 9 Apr 2003 11:12:12 -0400

> -----Original Message-----
> From: Richard Sherman [mailto:sherman@syr.edu] 
> Sent: Wednesday, April 09, 2003 10:39 AM
> To: statalist@hsphsun2.harvard.edu
> Subject: st: -reshape-, or -by-?
> 
> 
> Dear statalist,
> 
> I have a data set of country-years over EU countries that 
> looks roughly
> like this:
> 
> country	year	var1	var2	...
> 
> Austria	1995	1	2
> ...		...	...	...
> Austria	2000	3	4
> ...		...	...	...
> ...		...	...	...
> UK		1973	9	10
> ...		...	...	...
> UK		2000	11	12
> 
> As a getting-to-know-Stata assignment, I had my students use 
> this data to
> construct a data set with values of the variables summed across the EU
> countries.  The idea was to create a data set that had the 
> total of each
> variable for the EU as a whole.
> 
> I had them use -reshape- and -egen rsum()- to do this.  This 
> method first
> reshapes the data like this:
> 
> year	var1Austria	var2Austria ...	var1UK		var2UK	...
> 
> 1973	.		.			9		10
> ...
> 2000	3		4			11		12
> 
> and then sums across var1*, var2*, etc., using a -for any- 
> construction.
> 
> I added that there might be a simpler method (perhaps 
> involving -by-), but
> none of them took me up on this.  Is there a simpler way to 
> do this kind of
> thing (perhaps involving -by-)?
> 
> Thanks for your help,
> 
> Richard

How about:

	foreach v of varlist var1 var2 ... {
	  egen `v'_sum = sum(`v'), by(year)
	}

--Nick Winter


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index