Hello everyone

I have match data (soccer) of several leagues, incl. timestamp, home team, away time, home team goals, away team goals, the difference, the winner, the looser and tie_a and tie_b equaling the name of the teams if the match is tied. Unfortunately, I am missing the number of wins / defeats and number of goals scored at the beginning of the match of the home and the away team. Thus, I want to calculate variables that indicate the number of wins, defeats, ties and number of goals scored of the home team and the away team (i.e. #wins_home_team, #wins_away_team, etc.) in this season up until the date of the match. The data is ordered by time. For the first match of Hull City, these variables should be zero (as it is the first match). For the second match of Hull City, I want the generated variables to capture the number of wins, defeats, goals starting from the first match. For the 5th match, if Hull City is the away team, the variable #wins_away_team should count the number of wins in the past 4 matches of Hull City, #defeats_away_team should count the number of defeats in the past 4 matches of Hull City and so on. Same with goals scored. In addition, the command should be individually run for several groups all in the same dataset, for instance premier_league_2017, premier_league_2018 or seriaA_2017 - these are the group identifier (you don't see them below as the original dataset has over 50 variables), consisting of the specific league and the season.

I would highly appreciate any suggestions on how to code this. Many thanks in advance and soon a nice weekend

Best regards

Code:
input long timestamp str21 date_gmt str37(home_team_name away_team_name) byte(home_team_goal_count away_team_goal_count) float difference str37(winner looser tie_a tie_b)
1471087800 "Aug 13 2016 - 11:30am" "Hull City"            "Leicester City"       2 1  1 "Hull City"            "Leicester City"       ""                     ""                    
1471096800 "Aug 13 2016 - 2:00pm"  "Crystal Palace"       "West Bromwich Albion" 0 1 -1 "West Bromwich Albion" "Crystal Palace"       ""                     ""                    
1471096800 "Aug 13 2016 - 2:00pm"  "Everton"              "Tottenham Hotspur"    1 1  0 ""                     ""                     "Everton"              "Tottenham Hotspur"   
1471096800 "Aug 13 2016 - 2:00pm"  "Burnley"              "Swansea City"         0 1 -1 "Swansea City"         "Burnley"              ""                     ""                    
1471096800 "Aug 13 2016 - 2:00pm"  "Southampton"          "Watford"              1 1  0 ""                     ""                     "Southampton"          "Watford"             
1471096800 "Aug 13 2016 - 2:00pm"  "Middlesbrough"        "Stoke City"           1 1  0 ""                     ""                     "Middlesbrough"        "Stoke City"          
1471105800 "Aug 13 2016 - 4:30pm"  "Manchester City"      "Sunderland"           2 1  1 "Manchester City"      "Sunderland"           ""                     ""                    
1471177800 "Aug 14 2016 - 12:30pm" "AFC Bournemouth"      "Manchester United"    1 3 -2 "Manchester United"    "AFC Bournemouth"      ""                     ""                    
1471186800 "Aug 14 2016 - 3:00pm"  "Arsenal"              "Liverpool"            3 4 -1 "Liverpool"            "Arsenal"              ""                     ""                    
1471287600 "Aug 15 2016 - 7:00pm"  "Chelsea"              "West Ham United"      2 1  1 "Chelsea"              "West Ham United"      ""                     ""                    
1471633200 "Aug 19 2016 - 7:00pm"  "Manchester United"    "Southampton"          2 0  2 "Manchester United"    "Southampton"          ""                     ""                    
1471692600 "Aug 20 2016 - 11:30am" "Stoke City"           "Manchester City"      1 4 -3 "Manchester City"      "Stoke City"           ""                     ""                    
1471701600 "Aug 20 2016 - 2:00pm"  "Tottenham Hotspur"    "Crystal Palace"       1 0  1 "Tottenham Hotspur"    "Crystal Palace"       ""                     ""                    
1471701600 "Aug 20 2016 - 2:00pm"  "West Bromwich Albion" "Everton"              1 2 -1 "Everton"              "West Bromwich Albion" ""                     ""                    
1471701600 "Aug 20 2016 - 2:00pm"  "Burnley"              "Liverpool"            2 0  2 "Burnley"              "Liverpool"            ""                     ""                    
1471701600 "Aug 20 2016 - 2:00pm"  "Swansea City"         "Hull City"            0 2 -2 "Hull City"            "Swansea City"         ""                     ""                    
1471701600 "Aug 20 2016 - 2:00pm"  "Watford"              "Chelsea"              1 2 -1 "Chelsea"              "Watford"              ""                     ""                    
1471710600 "Aug 20 2016 - 4:30pm"  "Leicester City"       "Arsenal"              0 0  0 ""                     ""                     "Leicester City"       "Arsenal"             
1471782600 "Aug 21 2016 - 12:30pm" "Sunderland"           "Middlesbrough"        1 2 -1 "Middlesbrough"        "Sunderland"           ""                     ""                    
1471791600 "Aug 21 2016 - 3:00pm"  "West Ham United"      "AFC Bournemouth"      1 0  1 "West Ham United"      "AFC Bournemouth"      ""                     ""                    
1472297400 "Aug 27 2016 - 11:30am" "Tottenham Hotspur"    "Liverpool"            1 1  0 ""                     ""                     "Tottenham Hotspur"    "Liverpool"           
1472306400 "Aug 27 2016 - 2:00pm"  "Leicester City"       "Swansea City"         2 1  1 "Leicester City"       "Swansea City"         ""                     ""                    
1472306400 "Aug 27 2016 - 2:00pm"  "Crystal Palace"       "AFC Bournemouth"      1 1  0 ""                     ""                     "Crystal Palace"       "AFC Bournemouth"     
1472306400 "Aug 27 2016 - 2:00pm"  "Everton"              "Stoke City"           1 0  1 "Everton"              "Stoke City"           ""                     ""                    
1472306400 "Aug 27 2016 - 2:00pm"  "Southampton"          "Sunderland"           1 1  0 ""                     ""                     "Southampton"          "Sunderland"