Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

  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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading