Coralie
Coralie

Reputation: 221

Loop over half-yearly dates

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?

Upvotes: 0

Views: 58

Answers (1)

Nick Cox
Nick Cox

Reputation: 37208

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

  1. Confusing foreach with forvalues.

  2. 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.

  3. Calls like yh(1933h1) are wrong. yh requires two numeric arguments separated by a comma.

  4. 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.

Upvotes: 1

Related Questions