These are the commands I have so far:
set more off
use cohort, clear
set seed 404507 //a random number, to keep for replication
forval i=1/777 { // 777 is a maximum practice id,
use cohort, clear
keep if pracid==`i'
qui tab suicide
if r(r)==2 {
sttocc, n(10) /* NB- i have inserted a loop here as I will demonstrate below */
save temp/matched`i' //, replace
}
}
use temp/matched1, clear
forval i=2/777 {
capture append using temp/matched`i'
save matched, replace
}
use cohort, clear
set seed 404507 //a random number, to keep for replication
forval i=1/777 { // 777 is a maximum practice id,
use cohort, clear
keep if pracid==`i'
qui tab suicide
if r(r)==2 {
sttocc, n(10) /* NB- i have inserted a loop here as I will demonstrate below */
save temp/matched`i' //, replace
}
}
use temp/matched1, clear
forval i=2/777 {
capture append using temp/matched`i'
save matched, replace
}
forval d=1/11 { /*I am trying to tell stata to repeat the iterations 11 times to get a total of 1 case + 10 controls= 11 */
by _set: generate clusters= _n /*trying to get the total number of cases and controls in each set*/
keep if clusters== `d'
format index dobirth transferoutdate %d
keep if (index- dobirth) >= 5475 /*age in days */
keep if transferoutdate > index
}
by _set: generate clusters= _n /*trying to get the total number of cases and controls in each set*/
keep if clusters== `d'
format index dobirth transferoutdate %d
keep if (index- dobirth) >= 5475 /*age in days */
keep if transferoutdate > index
}
0 Response to stuck with nested loops
Post a Comment