'How do I order an event study (panel data) dataframe?

I have a big panel database ordered by month and subject with lots of time series variables. In this database several events occure to some of the subjects indicated by a dummy-like variable but with the number of the event.

So I have:

Month Subject_id event Variable_1 Variable_2
01-01-1970 A 0 8% 13%
02-01-1970 A 1 9% 5%
... ... ... ... ...
12-01-1984 B 0 -2% 1%
01-01-1985 B 2 10% 7%
02-01-1985 B 3 26% 3%

I want to construct another database where I can have the months ordered by before-after the event like t-12; t-11; t-10...t; t+1;t+2...

Month Event Subject_id Variable_1 Variable_2
t-1 1 A 8% 13%
t 1 A 9% 5%
... ... ... ... ...
t-1 2 B -2% 1%
t 2 B 10% 7%
... ... ... ... ...
t-1 3 B 10% 7%
t 3 B 26% 3%

Note that january 1985 is, at the same time, t for event 2 of subject B and t-1 for event 3 of the same subject. For this reason, I couldn't been able to merge by subject and a t+-x column. Some subjects have more than one overlapping event.

How can I transform my data into this new dataframe (I don't care about loosing the subjects that do not have events) ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source