Search code examples
stata

How to generate dummy variables from two categorical variables?


I have two variables containing state identifier and year. If I want to create dummy variables indicating each state, I usually write the following code:

tab state_id, gen(state_id_)

This will give me a group of variables, state_id_1,state_id_2,... etc. But what operations are available if I want to get a list of dummy variables for the interaction of state and year, for instance a dummy variable indicating state 1 in year 2005.


Solution

  • Have you tried looking at xi (https://www.stata.com/manuals13/rxi.pdf)? It will create dummies for each of the categorical variables and for the interaction of those two. So if you do:

     xi i.state*i.year
    

    This should give you what you are looking for, but note that it will naturally code this and omit the first category of each of your categorical variables.