Assume that we have a database with the following variables: id, year, treated, x1, and x2.

In this database, we would like to identify observations (id) based on a matching algorithm. For example, for a treated entity (treated = 1), you need to match another firm from the sample such that:

1. the other firm is not treated for a period [-5, 5] years based on the year of the treated company. For example, if a company is treated at t = 1990, the matched company should not be treated in the period [1985, 1995].

2. Based on x1, the matched company should have values on a specific range, say [70, 100].

3. The treated company's x2 is closest to the x2 of the matched company.

See, for example, the following database:

Code:
input id year treated x1 x2
1    1980    0    137    3
1    1981    0    82    3
1    1982    0    67    2
1    1983    0    41    2
1    1984    0    72    3
1    1985    0    37    4
1    1986    0    134    1
1    1987    0    44    4
1    1988    0    52    4
1    1989    0    49    1
1    1990    1    93    2
1    1991    0    121    4
1    1992    0    137    1
1    1993    0    138    4
1    1994    0    56    4
1    1995    0    112    1
1    1996    0    24    3
1    1997    0    35    1
1    1998    0    80    1
1    1999    0    64    1
1    2000    0    47    1
2    1980    0    54    2
2    1981    0    71    1
2    1982    0    110    4
2    1983    0    76    1
2    1984    0    125    2
2    1985    0    121    1
2    1986    0    58    3
2    1987    0    72    3
2    1988    0    87    2
2    1989    0    33    2
2    1990    0    29    4
2    1991    0    129    4
2    1992    1    96    3
2    1993    0    105    1
2    1994    0    91    1
2    1995    0    72    1
2    1996    0    134    3
2    1997    0    68    1
2    1998    0    33    4
2    1999    0    94    4
2    2000    0    77    3
3    1980    0    56    2
3    1981    0    88    2
3    1982    0    73    2
3    1983    0    31    3
3    1984    1    74    4
3    1985    0    133    2
3    1986    0    77    4
3    1987    0    112    4
3    1988    0    22    1
3    1989    0    74    4
3    1990    0    81    2
3    1991    0    31    3
3    1992    0    23    4
3    1993    0    73    3
3    1994    0    54    3
3    1995    0    115    1
3    1996    1    30    1
3    1997    0    61    2
3    1998    1    61    3
3    1999    0    90    3
3    2000    0    60    1
4    1980    0    22    5
4    1981    0    74    3
4    1982    0    81    4
4    1983    0    31    2
4    1984    0    23    8
4    1985    0    73    6
4    1986    0    54    7
4    1987    0    56    1
4    1988    0    130    3
4    1989    0    85    1
4    1990    0    75    9
4    1991    0    62    8
4    1992    0    15    8
4    1993    0    65    7
4    1994    0    93    5
4    1995    0    22    3
4    1996    0    11    2
4    1997    0    10    7
4    1998    0    29    9
4    1999    0    160    8
4    2000    0    5    5
end
If we would like to match entity with id = 1, we have to exclude that one with id = 2 because it has an event at t = 1992, which is in a time frame we do not like. Entities 3 and 4 are potential choices because both have an x1 in [70, 100]. However, we choose entity 4, because its x2 is closer that that of id = 1.

Apparently, this is a simple scenario and one can see that there may be multiple treatment cases within an entity (see for example entity 3).

I am asking whether there is a code that can generate a new variable (new column) that shows the firm that is matched. That is, for id = 1 this column should have value 4.

Thank you.