Timo K
Timo K

Reputation: 29

How to include interaction term in global macro for use in the reghdfe command?

I want to create an interaction term in the community-contributed regdhdfe command.

One of the two interaction variables is a continuous variable and used with a global. The other variable is a gender dummy.

My code looks something like this:

clear all
set obs 25000

local vlist v0 v1 v2 v3 v4 v5 v6 v7 v8 z4 z5 z6 

foreach v of local vlist { 
    generate `v' = runiform()
}


rename v8 i_female 
replace i_female=0 if i_female<0.5
replace i_female=1 if i_female>=0.5

foreach num of numlist 4 5 6 {
    global varlist_`num' "v`num' z`num'"
}

foreach num of numlist 4 5 6 {
    reghdfe v0 ${varlist_`num'} ${varlist_`num'}#i.i_female if v1<0.8, absorb(v7)
}

This returns the following error:

error: there are repeated variables:
r(198);

Upvotes: 0

Views: 714

Answers (1)

user8682794
user8682794

Reputation:

What you want and what you instruct Stata to do in the above code are two different things.

Stata complains that there are repeated variables because when the macros expand you do not get v4 z4 v4*i_female z4*i_female.

Instead the variables v4, v5 and v6 are duplicated:

foreach num of numlist 4 5 6 {
    global varlist_`num' "v`num' z`num'"
}

foreach num of numlist 4 5 6 {
    display "v0 ${varlist_`num'} ${varlist_`num'}#i.i_female if v1<0.8, absorb(v7)"
}

v0 v4 z4 v4 z4#i.i_female if v1<0.8, absorb(v7)
v0 v5 z5 v5 z5#i.i_female if v1<0.8, absorb(v7)
v0 v6 z6 v6 z6#i.i_female if v1<0.8, absorb(v7)

In order to get the desired output, you need to modify your code as follows:

foreach num of numlist 4 5 6 {
    global varlist1_`num' "v`num' z`num'"
    global varlist2_`num' "v`num'#i.i_female z`num'#i.i_female"
}

foreach num of numlist 4 5 6 {
    display "v0 ${varlist1_`num'} ${varlist2_`num'} if v1<0.8, absorb(v7)"
}

v0 v4 z4 v4#i.i_female z4#i.i_female if v1<0.8, absorb(v7)
v0 v5 z5 v5#i.i_female z5#i.i_female if v1<0.8, absorb(v7)
v0 v6 z6 v6#i.i_female z6#i.i_female if v1<0.8, absorb(v7)

Upvotes: 2

Related Questions