I am trying to generate a set of dummies for two variables (using the same set of dummy). My data set looks like this:
The first two columns are the variables I have, they are the two parties in a game (e.g. 5 teams). I need to generate 5 dummy variables to indicate whether the team is playing in the game (as shown in the left columns. Teamdummy1 is a. Teamdummy2 is b, etc.)
Team 1 | Team2 | Teamdummy1 | Teamdummy2 | Teamdummy3 | Teamdummy4 | Teamdummy5 |
a | b | 1 | 1 | 0 | 0 | 0 |
b | c | 0 | 1 | 1 | 0 | 0 |
c | a | 1 | 0 | 1 | 0 | 0 |
d | e | 0 | 0 | 0 | 1 | 1 |
e | d | 0 | 0 | 0 | 1 | 1 |
sort Team1
tabulate Team1, generate(Team1dummy)
sort Team2
tabulate Team2, generate(Team2dummy)
gen Teamdummy1=max(Team1dummy1, Team2dummy1)
gen Teamdummy2=max(Team1dummy2, Team2dummy2)
gen Teamdummy3=max(Team1dummy3, Team2dummy3)
gen Teamdummy4=max(Team1dummy4, Team2dummy4)
gen Teamdummy5=max(Team1dummy5, Team2dummy5)
However, as I have 30 teams, I am just wondering whether I can use a loop to generate the dummies instead of writing it for 30 times. Thanks in advance!
0 Response to Generate dummy variables using loops
Post a Comment