Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 72762

Calculating a mean within a time period

$
0
0
Dear Statalist members,

I have a data sample in which I want to calculate the mean (among other things) within a time window of 1.5 years before and 1.5 years after the announcement date. The observations also have to be within the same industry (SIC code). My data sample looks like this:

:
clear
input str4 tsic str10 date float(DateAnn FSPUebitdam FSPUtassets)
"1021" "11-19-2006" 17124 5.8200002 15270.264
"1041" "06-21-2000" 14782 14.81 615.62207
"1041" "06-25-2001" 15151 15.37 1729.0763
"1041" "02-04-2002" 15375 26.16 416.08307
"1041" "09-27-2002" 15610 293.67001 9.1161404
"1041" "04-27-2005" 16553 . 192.69046
"1041" "07-15-2005" 16632 . .
"1041" "08-31-2006" 17044 107.818 753.26996
"1041" "02-05-2007" 17202 . 136.82915
"1041" "03-05-2007" 17230 68.138 33.648335
end
In another post I found this, which enables to calculate the mean of the previous 1.5 year:

:
sort tsic DateAnn
local more 1
local i 0
local vtype : type FSPUebitdam
local window = 365 * 1.5
while `more' {
    by tsic: gen `vtype' pv_`++i' = FSPUebitdam[_n-`i'] ///
        if (DateAnn - DateAnn[_n-`i']) < `window'
    qui count if !mi(pv_`i')
    local more = r(N)
}
However, as this does not include the 1.5 years after the announcement date I added this as another requirement:

:
| (DateAnn[_n+`i'] - DateAnn) < `window'
It also doesn't include the value of the current observation, that's why I reformulated the code above to:

:
by tsic: gen `vtype' pv_`++i' = FSPUebitdam[_n-`i'] + FSPUebitdam///
However, both my adjustment unfortunately do not work. Optionally, I would like to add another requirement, the assets have to be within a range of -1.5x +1.5x of the current observation.
I hope someone can help me on this.

Best,
Max

Viewing all articles
Browse latest Browse all 72762

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>