create a set of continuous variables from a factor variable and a continuous variable

StackOverflow https://stackoverflow.com/questions/23576256

  •  19-07-2023
  •  | 
  •  

Question

In Stata, I have a factor variable with 50 levels (state) and an integer-valued variable (year). I want to create 50 new variables: 50 interactions of state indicators with the year variable. Is there a way to do this without writing 50 lines of code?

I can produce the 50 state dummies with tabulate state, generate (state), but I don't know how to get further than that without writing a line to create each individual state-year variable.

I want to use the new state-year variables in a regression. Stata's factor notation makes it easy to include the state-year variables as regressors without creating them beforehand (e.g., with a command like regress y i.state#c.year), but some add-on functions don't support factor notation.

Was it helpful?

Solution

You can try using xi, both as a stand-alone command to create indicator and interaction terms, and as a command prefix. A nonsensical example:

clear all
set more off

sysuse auto

* stand-alone
xi i.rep78*mpg

* as prefix
xi: regress price i.rep78*mpg

Run help xi for all the details.

Edit

To make this a bit clearer, suppose the regress command did not admit the use of either factor variable notation or the xi: prefix. Then using the xi stand-alone syntax you could create the indicator and interaction terms (which answers your original question) and then use those terms with the regress command:

sysuse auto, clear

xi i.rep78*mpg
regress price mpg _Irep78* _IrepXmpg*

(Remember to use Stata's help capabilities. Running search interactions, for example, leads you to xi......Interaction expansion.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top