Hi everyone. I am a Matlab user new to Stata and I have a question.
I have a dataset with more than 150 thousand observations. It is of the following form:
I want to create a new variable called newquarter and do the following: if the meeting is annual newquarter = quarter
if the meeting is notAnnual newquarter equals the quarter of an annual observation in the same eventyear. I have the following code (or rather algorithm) which I know won't work due to syntax and is probably not efficient:
gen identifier = .
replace identifier = _n if notAnnual == 1
foreach identifier != . {
tempid = identifier-5/identifier+5
foreach tempid <= identifier+5 {
if (CompanyID[identifier] == CompanyID[tempid] & eventyear[identifier] == eventyear[tempid]) {
replace newquarter[identifier] = quarter[tempid]
tempid = tempid + 6
}
}
}
I appreciate it if you could help me with this.
Thanks.
I have a dataset with more than 150 thousand observations. It is of the following form:
CompanyID | eventyear | quarter | notAnnual |
152440 | 2009 | 3 | 0 |
152440 | 2009 | 3 | 0 |
152440 | 2009 | 3 | 0 |
152440 | 2009 | 4 | 1 |
152440 | 2009 | 4 | 1 |
152455 | 2008 | 1 | 0 |
152455 | 2008 | 1 | 0 |
152455 | 2008 | 1 | 0 |
152455 | 2008 | 1 | 0 |
152455 | 2009 | 1 | 0 |
152455 | 2009 | 1 | 0 |
152455 | 2010 | 1 | 0 |
152455 | 2010 | 1 | 0 |
152455 | 2010 | 1 | 0 |
152455 | 2011 | 1 | 0 |
152455 | 2011 | 1 | 0 |
152455 | 2011 | 3 | 1 |
152455 | 2012 | 1 | 0 |
152455 | 2012 | 1 | 0 |
if the meeting is notAnnual newquarter equals the quarter of an annual observation in the same eventyear. I have the following code (or rather algorithm) which I know won't work due to syntax and is probably not efficient:
gen identifier = .
replace identifier = _n if notAnnual == 1
foreach identifier != . {
tempid = identifier-5/identifier+5
foreach tempid <= identifier+5 {
if (CompanyID[identifier] == CompanyID[tempid] & eventyear[identifier] == eventyear[tempid]) {
replace newquarter[identifier] = quarter[tempid]
tempid = tempid + 6
}
}
}
I appreciate it if you could help me with this.
Thanks.
Comment