I am running an event study in Stata and I would like to interact each of my dates with a dummy variable. The dates are divided into semesterd, i.e., half years, %th format.
Here below what I tried:
foreach yh = yh(1933h1)(1)yh(1958h2){
gen d_`yh'=dummy*(sdate==`yh')
label var d_`yh' "`yh'"
}
Do you have any idea of how I could proceed?
>Solution :
Try
forval yh = `= yh(1933, 1)'/`= yh(1958, 2)' {
local YH : subinstr local yh "-" "_", all
gen d_`YH' = dummy*(sdate==`yh')
label var d_`YH' "`yh'"
}
or
forval yh = -58/-3 {
local YH : subinstr local yh "-" "_", all
gen d_`YH' = dummy*(sdate==`yh')
label var d_`YH' "`yh'"
}
The errors in your code are
-
Confusing
foreachwithforvalues. -
Either loop expects to see constants here determining the limits. You can evaluate an expression on the fly, but not as you tried to do it.
-
Calls like
yh(1933h1)are wrong.yhrequires two numeric arguments separated by a comma. -
The half-yearly dates you are looping over are all negative, as occurring before 1960. But minus signs can’t be part of variable names.