Bookmark and Share

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: forvalues + egen rsum with missing variables


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: forvalues + egen rsum with missing variables
Date   Thu, 6 Jan 2011 17:23:10 +0000

-rsum()- is an old and undocumented synonym for what is now called
-rowtotal()-. But it still works.

I have not really tried to understand this, as I think your posting
implies one major piece of advice.

It looks as if you need to -reshape long-. After that, not only this
but many other tasks of data management and statistical analysis will
be much easier and this messy problem will just reduce to one
involving -by:-.

Nick

On Fri, Jan 7, 2011 at 1:10 AM, Florian Seliger <florian.seliger@gmx.de> wrote:

> I'm trying to use the following command on a couple of datasets:
>
> egen id_max=max(id)
> local l=id_max
>
> forvalues j=1960(1)2009 {       //years
> forvalues k=1(1)`l' {                   //number of IDs
> egen a_`j'=rsum(a_`j'_`k' - a_`j'_`l')
>        }
>        }
>
> ********
>
>
> The problem is that a dataset doesn't contain all years from 1960 to 2009, but only a subset, e.g. 1961, 1985, 1994 etc. In addition, the number of IDs varies (therefore, I need `l' as a maximum) and each year can only be assigned a subset of IDs (they are not assigned in a consistent way).
> To give an example, I might have the following variables in the dataset:
> a_1990_63
> a_1990_64
> a_1990_65
> a_1990_66
> a_1990_67
> a_1990_68
> a_1990_69
>
>  a_1994_1
> a_1994_2
> a_1994_3
> a_1994_4
> a_1994_5
> a_1994_6
> a_1994_7
> a_1994_8
> a_1994_9        ... etc.
>
> How can I proceed in order to calculate the row sum for each year? I have tried to use "capture", but it didn't work.

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index