Dan Daugaard
On reflection, the advice I've given above is going in the wrong direction.
As with so many things in Stata these problems appear complicated only because your data is organized in a wide layout. If you switch to a long layout, they become easy, as the example code below shows.
If there is some compelling reason you need to go back to wide layout, you can do that at the point that it is required. But think twice before doing that. There are only a small number of things in Stata that are easier to do with wide data. Until you know you're going to do one of them, its best to stick with a long layout.
Here are the data I used, reverse engineered from your example in post #14.
On reflection, the advice I've given above is going in the wrong direction.
As with so many things in Stata these problems appear complicated only because your data is organized in a wide layout. If you switch to a long layout, they become easy, as the example code below shows.
If there is some compelling reason you need to go back to wide layout, you can do that at the point that it is required. But think twice before doing that. There are only a small number of things in Stata that are easier to do with wide data. Until you know you're going to do one of them, its best to stick with a long layout.
Code:
use events.dta, clear generate i = 1 // to match to master sort event_month // replace the event text with a numeric id and value label rename event eventname encode eventname, generate(event) label list event drop eventname tempfile events save `events' use master.dta generate i = 1 // to match to events joinby i using `events' drop i order id event event_month generate diff = month-event_month bysort diff: egen avg_ff = mean(ff) if inlist(diff,-2,-1,0,1,2) egen toprint = tag(diff) list diff avg_ff if toprint & inlist(diff,-2,-1,0,1,2), noobs drop toprint sort id event month list if id==2708 & event==2, noobs abbreviate(12) describe
Code:
. label list event event: 1 event_1 2 event_2
Code:
. list diff avg_ff if toprint & inlist(diff,-2,-1,0,1,2), noobs +------------------+ | diff avg_ff | |------------------| | -2 -.0023612 | | -1 -.002323 | | 0 -.0020188 | | 1 -.0008208 | | 2 -.0016764 | +------------------+
Code:
. list if id==2708 & event==2, noobs abbreviate(12) +------------------------------------------------------------------------+ | id event event_month month ff diff avg_ff | |------------------------------------------------------------------------| | 2708 event_2 2015 Jan 2014 Oct -.005559 -3 . | | 2708 event_2 2015 Jan 2014 Nov -.0023612 -2 -.0023612 | | 2708 event_2 2015 Jan 2014 Dec .000913 -1 -.002323 | | 2708 event_2 2015 Jan 2015 Jan -.0016764 0 -.0020188 | | 2708 event_2 2015 Jan 2015 Feb -.0025547 1 -.0008208 | +------------------------------------------------------------------------+ . describe Contains data obs: 20 vars: 7 size: 560 ------------------------------------------------------------------------------------------------ storage display value variable name type format label variable label ------------------------------------------------------------------------------------------------ id long %12.0g event long %8.0g event event_month float %tm.. month float %tm.. ff float %9.0g diff float %9.0g avg_ff float %9.0g ------------------------------------------------------------------------------------------------ Sorted by: id event month
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input long id float(month ff) 2708 657 -.005559 2708 658 -.0023612394 2708 659 .0009129993 2708 660 -.0016764477 2708 661 -.0025546604 2709 657 -.005559 2709 658 -.0023612394 2709 659 .0009129993 2709 660 -.0016764477 2709 661 -.0025546604 end format %tmCCYY_Mon month save master, replace clear input float event_month str16 event 658 "event_1" 660 "event_2" end format %tmCCYY_Mon event_month save events, replace
Comment