Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: FW: forvalues question...
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: FW: forvalues question...
Date
Fri, 8 Feb 2013 14:18:38 +0000
I take it that you want another way of getting a -collapse- without
typing out all the variable names.
Your tentative code is a long way from legal syntax, but the spirit is right.
Code might be
local call (sum)
forval j = 1/20 {
local call `call' var`j'_s=var`j'
}
collapse `call', by(year)
Nick
On Fri, Feb 8, 2013 at 2:04 PM, Mahometa, Michael J
<[email protected]> wrote:
> 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.
>
> Is there a solution like forvalues? Something like:
> collapse (sum) ///
> forvalues num = 1(1)20 { ///
> var'num'_s = var'num' ///
> }, by(year)
>
> (Which doesn't work.)
*
* 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/