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: Averages between dates that are different for each agent


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Averages between dates that are different for each agent
Date   Thu, 9 Jan 2014 17:37:50 +0000

This is easy with something like

egen mean_var = mean(var / inrange(period, startdate, enddate)), by(agent)

Here the -inrange()-  call produces 1 or 0. Dividing by 1 leaves
values unchanged; dividing by 0 produces missings, which are ignored.

For more discussion, see
http://www.stata-journal.com/article.html?article=dm0055

Nick
[email protected]

On 9 January 2014 16:21, Miguel A. Duran <[email protected]> wrote:

>  In a panel dataset, I would like to calculate the average value of
> a variable for each agent, but the number of periods to be considered is
> different for each agent. The following example might help to understand
> what I mean (and to show how my dataset is, in a simplified version,
> structured):
>
> Agent   Period      Var    Startdate   Enddate
>   1              t1          11           t2               t3
>   1              t2          12           t2               t3
>   1              t3          13           t2               t3
>   1              t4          14           t2               t3
>   2              t1          21           t1               t4
>   2              t2          22           t1               t4
>   2              t3          23           t1               t4
>   2              t4          24           t1               t4
>
> According to what startdate and endate indicate, I would like to calculate
> the average of var for agent 1 between t2 and t3, whereas for agent 2 the
> period to calculate the average of var goes from t1 to t4.
*
*   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/


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