Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Adding up prior iterations of a variable in a time series dataset

From   Nick Cox <>
Subject   Re: st: Adding up prior iterations of a variable in a time series dataset
Date   Sun, 24 Jun 2012 16:27:52 +0100

Cumulative sums are usually best calculated with the -sum()- function.

What you want may be more like

bysort page_id (time_var) : gen clicks_to_date = sum(clicks)


On Sun, Jun 24, 2012 at 3:41 PM, Benedikt Achatz
<> wrote:

> I am a student and rather new to Stata, and i just can't figure out how this
> works. I have been browsing the web for some time and haven't found a
> solution that worked for me.
> I have a dataset that contains observations of websites at different points
> in time. There is a variable, clicks, that contains the clicks in one time
> period. What I need to do now is to create a variable clicks_to_date that
> contains the clicks in every time period before the current one, including
> that one.
> This is the code I have been using thus far, without much success...
>   levelsof page_id, local(pg_id)
>   levelsof time_var, local(tm_vr)
>   cap drop clicks_to_date
>   gen clicks_to_date = 0
>   local clicks_old = 0
>   foreach id in `pg_id'{
>        foreach x in `tm_vr'{
>        if `x' == 1 {local clicks_old = 0
>        }
>        replace clicks_to_date = clicks+`clicks_old'
>        display `clicks_old'
>        local clicks_old = clicks_to_date
>        }
>   }

*   For searches and help try:

© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index