I am stuck on a very particular problem and I am sure there needs to be a way to solve it.

The data is on European football (see sample below) and contains league tables from mid-season ("MID") and end of season ("END"). In the original data set, there are many different leagues from a range of different countries. I want to analyse 'end of season' performance of teams that are around the 'relegation line' by mid-season.
The dummy variable "relegation" marks the relegation ranks. The dummy "relegation_indic" marks the pair of teams around the 'relegation line'. The variable "club_id" identifies every single club in the data set (across different countries).

Selecting the teams of interest by mid-season is easy as they are defined by the dummy "relegation_indic". Now I want to be able to select these two teams in the end of the season table for the respective season.

In my opinion, the "club_id" variable should be used to identify the two teams at mid season ("relegation_indic" = 1) and then the two corresponding observation with "END" could be selected as well. All of this within the group defined by "panelVAR". The 'keep' and 'drop' commands seem insufficient to tackle this problem. As STATA has no commands like 'take' or 'where' and I have not found any usable code online, I am posting on Statalist for the first time.

In the end, there should be four observations for every season in every league containing the two teams defined at mid-season and the two observations of the same teams at the end of season.

Thanks in advance.

Kind regards,
Yannick


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str3 Country str24 League str9 Season str3 PISMIDEND str36 yearVAR str39 identify byte Rank float(relegation relegation_indic) str6 club_id str25 Club str2 GamesCount
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END"  1 0 0 "UKR32" "Ilyichivets"       "38"
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END"  2 0 0 "UKR57" "PFK Lviv"          "38"
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END"  3 0 0 "UKR53" "Obolon Kiew"       "38"
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END"  4 0 0 "UKR9"  "Desna"             "38"
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END"  5 0 0 "UKR13" "Dynamo 2 Kiew"     "38"
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END"  6 0 0 "UKR31" "Igroservis"        "38"
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END"  7 0 0 "UKR68" "Stal Alchevsk"     "38"
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END"  8 0 0 "UKR58" "PFK Oleksandria"   "38"
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END"  9 0 0 "UKR74" "Volyn Lutsk"       "38"
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END" 10 0 0 "UKR44" "MFK Mykolaiv"      "38"
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END" 11 0 0 "UKR42" "Krymteplitsa"      "38"
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END" 12 0 0 "UKR11" "Dnister"           "38"
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END" 13 0 0 "UKR15" "Enerhetyk"         "38"
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END" 14 0 0 "UKR29" "Helios Kharkiv"    "38"
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END" 15 0 0 "UKR21" "FK Sevastopol"     "38"
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END" 16 0 0 "UKR24" "Feniks"            "38"
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END" 17 0 1 "UKR61" "Prykarpattia"      "38"
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END" 18 1 1 "UKR36" "Khodak Cherkasy"   "38"
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END" 19 1 0 "UKR76" "ZSKA Kiew"         "38"
"UKR" "Persha Liga" "2007/2008" "END" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008END" 20 1 0 "UKR70" "Stal D."           "38"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID"  1 0 0 "UKR53" "Obolon Kiew"       "19"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID"  2 0 0 "UKR33" "Ilyichivets  "   "19"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID"  3 0 0 "UKR13" "Dynamo 2 Kiew"     "19"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID"  4 0 0 "UKR57" "PFK Lviv"          "19"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID"  5 0 0 "UKR58" "PFK Oleksandria"   "19"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID"  6 0 0 "UKR74" "Volyn Lutsk"       "19"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID"  7 0 0 "UKR9"  "Desna"             "19"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID"  8 0 0 "UKR44" "MFK Mykolaiv"      "19"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID"  9 0 0 "UKR42" "Krymteplitsa"      "19"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID" 10 0 0 "UKR69" "Stal Alchevsk  " "19"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID" 11 0 0 "UKR31" "Igroservis"        "19"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID" 12 0 0 "UKR22" "FK Sevastopol  " "19"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID" 13 0 0 "UKR12" "Dnister  "       "19"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID" 14 0 0 "UKR29" "Helios Kharkiv"    "19"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID" 15 0 0 "UKR15" "Enerhetyk"         "19"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID" 16 0 0 "UKR61" "Prykarpattia"      "19"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID" 17 0 1 "UKR25" "Feniks  "        "19"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID" 18 1 1 "UKR36" "Khodak Cherkasy"   "19"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID" 19 1 0 "UKR70" "Stal D."           "19"
"UKR" "Persha Liga" "2007/2008" "MID" "UKRPersha Liga2007/2008" "UKRPersha Liga2007/2008MID" 20 1 0 "UKR76" "ZSKA Kiew"         "19"
"UKR" "Persha Liga" "2008/2009" "END" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009END"  1 0 0 "UKR77" "Zakarpattia"       "32"
"UKR" "Persha Liga" "2008/2009" "END" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009END"  2 0 0 "UKR53" "Obolon Kiew"       "32"
"UKR" "Persha Liga" "2008/2009" "END" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009END"  3 0 0 "UKR58" "PFK Oleksandria"   "32"
"UKR" "Persha Liga" "2008/2009" "END" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009END"  4 0 0 "UKR21" "FK Sevastopol"     "32"
"UKR" "Persha Liga" "2008/2009" "END" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009END"  5 0 0 "UKR74" "Volyn Lutsk"       "32"
"UKR" "Persha Liga" "2008/2009" "END" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009END"  6 0 0 "UKR42" "Krymteplitsa"      "32"
"UKR" "Persha Liga" "2008/2009" "END" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009END"  7 0 0 "UKR13" "Dynamo 2 Kiew"     "32"
"UKR" "Persha Liga" "2008/2009" "END" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009END"  8 0 0 "UKR9"  "Desna"             "32"
"UKR" "Persha Liga" "2008/2009" "END" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009END"  9 0 0 "UKR11" "Dnister"           "32"
"UKR" "Persha Liga" "2008/2009" "END" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009END" 10 0 0 "UKR68" "Stal Alchevsk"     "32"
"UKR" "Persha Liga" "2008/2009" "END" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009END" 11 0 0 "UKR31" "Igroservis"        "32"
"UKR" "Persha Liga" "2008/2009" "END" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009END" 12 0 0 "UKR50" "Naftovyk"          "32"
"UKR" "Persha Liga" "2008/2009" "END" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009END" 13 0 0 "UKR24" "Feniks"            "32"
"UKR" "Persha Liga" "2008/2009" "END" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009END" 14 0 0 "UKR15" "Enerhetyk"         "32"
"UKR" "Persha Liga" "2008/2009" "END" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009END" 15 0 0 "UKR29" "Helios Kharkiv"    "32"
"UKR" "Persha Liga" "2008/2009" "END" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009END" 16 0 0 "UKR61" "Prykarpattia"      "32"
"UKR" "Persha Liga" "2008/2009" "END" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009END" 17 0 1 "UKR37" "Knyazha"           "32"
"UKR" "Persha Liga" "2008/2009" "END" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009END" 18 1 1 "UKR40" "Komunalnik"        "-" 
"UKR" "Persha Liga" "2008/2009" "MID" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009MID"  1 0 0 "UKR53" "Obolon Kiew"       "18"
"UKR" "Persha Liga" "2008/2009" "MID" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009MID"  2 0 0 "UKR77" "Zakarpattia"       "18"
"UKR" "Persha Liga" "2008/2009" "MID" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009MID"  3 0 0 "UKR21" "FK Sevastopol"     "18"
"UKR" "Persha Liga" "2008/2009" "MID" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009MID"  4 0 0 "UKR31" "Igroservis"        "18"
"UKR" "Persha Liga" "2008/2009" "MID" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009MID"  5 0 0 "UKR68" "Stal Alchevsk"     "18"
"UKR" "Persha Liga" "2008/2009" "MID" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009MID"  6 0 0 "UKR74" "Volyn Lutsk"       "18"
"UKR" "Persha Liga" "2008/2009" "MID" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009MID"  7 0 0 "UKR9"  "Desna"             "18"
"UKR" "Persha Liga" "2008/2009" "MID" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009MID"  8 0 0 "UKR58" "PFK Oleksandria"   "18"
"UKR" "Persha Liga" "2008/2009" "MID" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009MID"  9 0 0 "UKR11" "Dnister"           "18"
"UKR" "Persha Liga" "2008/2009" "MID" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009MID" 10 0 0 "UKR50" "Naftovyk"          "17"
"UKR" "Persha Liga" "2008/2009" "MID" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009MID" 11 0 0 "UKR42" "Krymteplitsa"      "17"
"UKR" "Persha Liga" "2008/2009" "MID" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009MID" 12 0 0 "UKR13" "Dynamo 2 Kiew"     "18"
"UKR" "Persha Liga" "2008/2009" "MID" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009MID" 13 0 0 "UKR24" "Feniks"            "18"
"UKR" "Persha Liga" "2008/2009" "MID" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009MID" 14 0 0 "UKR37" "Knyazha"           "18"
"UKR" "Persha Liga" "2008/2009" "MID" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009MID" 15 0 0 "UKR29" "Helios Kharkiv"    "18"
"UKR" "Persha Liga" "2008/2009" "MID" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009MID" 16 0 0 "UKR61" "Prykarpattia"      "18"
"UKR" "Persha Liga" "2008/2009" "MID" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009MID" 17 0 1 "UKR15" "Enerhetyk"         "18"
"UKR" "Persha Liga" "2008/2009" "MID" "UKRPersha Liga2008/2009" "UKRPersha Liga2008/2009MID" 18 1 1 "UKR40" "Komunalnik"        "-" 
"UKR" "Persha Liga" "2009/2010" "END" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010END"  1 0 0 "UKR74" "Volyn Lutsk"       "34"
"UKR" "Persha Liga" "2009/2010" "END" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010END"  2 0 0 "UKR21" "FK Sevastopol"     "34"
"UKR" "Persha Liga" "2009/2010" "END" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010END"  3 0 0 "UKR68" "Stal Alchevsk"     "34"
"UKR" "Persha Liga" "2009/2010" "END" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010END"  4 0 0 "UKR57" "PFK Lviv"          "34"
"UKR" "Persha Liga" "2009/2010" "END" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010END"  5 0 0 "UKR58" "PFK Oleksandria"   "34"
"UKR" "Persha Liga" "2009/2010" "END" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010END"  6 0 0 "UKR42" "Krymteplitsa"      "34"
"UKR" "Persha Liga" "2009/2010" "END" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010END"  7 0 0 "UKR50" "Naftovyk"          "34"
"UKR" "Persha Liga" "2009/2010" "END" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010END"  8 0 0 "UKR9"  "Desna"             "34"
"UKR" "Persha Liga" "2009/2010" "END" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010END"  9 0 0 "UKR2"  "Arsenal B. T."     "34"
"UKR" "Persha Liga" "2009/2010" "END" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010END" 10 0 0 "UKR79" "Zirka"             "34"
"UKR" "Persha Liga" "2009/2010" "END" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010END" 11 0 0 "UKR29" "Helios Kharkiv"    "34"
"UKR" "Persha Liga" "2009/2010" "END" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010END" 12 0 0 "UKR11" "Dnister"           "34"
"UKR" "Persha Liga" "2009/2010" "END" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010END" 13 0 0 "UKR13" "Dynamo 2 Kiew"     "34"
"UKR" "Persha Liga" "2009/2010" "END" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010END" 14 0 0 "UKR24" "Feniks"            "34"
"UKR" "Persha Liga" "2009/2010" "END" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010END" 15 0 0 "UKR15" "Enerhetyk"         "34"
"UKR" "Persha Liga" "2009/2010" "END" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010END" 16 0 1 "UKR61" "Prykarpattia"      "34"
"UKR" "Persha Liga" "2009/2010" "END" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010END" 17 1 1 "UKR16" "FK Kharkiv"        "34"
"UKR" "Persha Liga" "2009/2010" "END" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010END" 18 1 0 "UKR51" "Nyva Ternopil"     "34"
"UKR" "Persha Liga" "2009/2010" "MID" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010MID"  1 0 0 "UKR21" "FK Sevastopol"     "17"
"UKR" "Persha Liga" "2009/2010" "MID" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010MID"  2 0 0 "UKR29" "Helios Kharkiv"    "17"
"UKR" "Persha Liga" "2009/2010" "MID" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010MID"  3 0 0 "UKR57" "PFK Lviv"          "17"
"UKR" "Persha Liga" "2009/2010" "MID" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010MID"  4 0 0 "UKR68" "Stal Alchevsk"     "17"
"UKR" "Persha Liga" "2009/2010" "MID" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010MID"  5 0 0 "UKR74" "Volyn Lutsk"       "17"
"UKR" "Persha Liga" "2009/2010" "MID" "UKRPersha Liga2009/2010" "UKRPersha Liga2009/2010MID"  6 0 0 "UKR58" "PFK Oleksandria"   "17"
end