Dear statalist,

I've experieced quite a challenge regarding the restructuring of my dataset and generating new variables. In this dataset there is each time a company (target) that is acquired by another company (acquiror), which was previously owned by a third company (Seller). Most of the acquired companies are then sold again by the initial acquirer to another company. Basically I have a dataset that looks as follows
TargetID AcquirorID SellerID Dealdate DealID
UK1 BE2 US3 21/06/1994 1
UK1 IE4 BE2 23/07/1997 2
BE5 US3 SL1 02/01/2002 3
BE5 IT4 US3 03/04/2004 4
BE5 UK8 IT4 05/06/2007 5
PO4 GI6 PO4 03/06/2009 6
PO4 US9 GI6 08/06/2010 7
I, however, want to restructure my dataset such that each observation specifies when a company acquires and sells a specific target, such that the dataset would look as follows:
AcquirorID TargetID Entrydate Exitdate EntrydealID ExitdealID
BE2 UK1 21/06/1994 23/07/1997 1 2
IE4 UK1 23/07/1997 N.A. 2 N.A.
US3 BE5 02/01/2002 03/04/2004 3 4
IT4 BE5 03/04/2004 05/06/2007 4 5
UK8 BE5 05/06/2007 N.A. 5 N.A.
GI6 PO4 03/06/2009 08/06/2010 6 7
IS9 PO4 08/06/2010 N.A. 7 N.A.
The entrydate would thus be the date where a company acquires a specific company, whereas the exit date would be equal to the Dealdate when the acquirer becomes the seller. A similar procedure would than be used to create the entrydeal ID and Exit dealID by using the DealID. The "N.A." would imply that the company has not yet been sold by the current owner.

I have only recently started using stata and know how to generate new variables based on existing ones. I generated the entry date and exit date ID by simply using the code below, which worked fine.
Code:
//code Entrydate//
gen Entrydate = Dealdate
format %tdnn/dd/CCYY Entrydate
recast int Entrydate
//code EntrydealID//
gen Entryid = DealID
However, I can not find how to operationalize the code for the ExitDate and ExitID. Could anyone help?

Thanks in advance.

Regards,

Gianni