Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# RE: st: FW: forvalues question...

 From "Dudekula, Anwar" To "statalist@hsphsun2.harvard.edu" Subject RE: st: FW: forvalues question... Date Fri, 8 Feb 2013 17:14:28 +0000

```Hi Mike

Can you use egen(sumperyear): rowsum(var1-var5)

Thank you,
Yours Sincerely,
Anwar
________________________________________
From: owner-statalist@hsphsun2.harvard.edu [owner-statalist@hsphsun2.harvard.edu] on behalf of Nick Cox [njcoxstata@gmail.com]
Sent: Friday, February 08, 2013 9:36 AM
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: FW: forvalues question...

Yes, that's better than my solution. I got hung up on how to do it
with -forvalues-, when a better answer is to do it directly.

Nick

On Fri, Feb 8, 2013 at 2:27 PM, Scott Merryman <scott.merryman@gmail.com> wrote:
> On Fri, Feb 8, 2013 at 8:04 AM, Mahometa, Michael J
> <michael.mahometa@ssc.utexas.edu> wrote:
>> All,
>>
>> I've got a dataset that has multiple rows for one year, with several sequential variables. And, I'd like to sum what happens over the year (using something like collapse). I have 20 variables and the data looks like:
>>
>> Year   var1  var2  var3  var4  var5 ....
>> 1999   2     5     6     1     4
>> 1999   4     1     5     2     5
>> 1999   1     3     5     6     7
>> 2000   6     5     1     3     8
>> 2000   1     1     2     3     6
>> ...
>>
>> So the current "brute force" method is:
>> collapse (sum) var1_s=var1 var2_s=var2 ///
>> var3_s=var3 var4_s=var4, by(year)
>>
>> But this seems in-elegant.
>
>
> collapse (sum) var*, by(year)
> rename var* =_s
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```