Dear Colleagues
I need to run a loop on a program which generates probabilities of 'spells' . The program inside the loop generates the probability of spells for one individual,and uses the bys command to generate the length of the spells and the the probabilities. I need to run a loop on it for a full dataset of individuals, where each individual has a person identification number (pid).
Below is my code. My understanding is that one cannot use the bys command in the loop, the program thus works very well without a loop, for one indiviudual, but doesn't within a loop. However, I need the bys command to be able to ask Stata to count the length of the spell for each group (hence the bys group).
Please can someone assist me with getting this code to run.
su pid, meanonly
forval i = 1/`r(max)' {
gen group= x != x[_n-1] if pid == `i'
replace group = sum(group) if pid == `i'
bys group: gen lengthspell =_N if pid == `i'
gen p_hat_spell = lengthspell/nyear if pid == `i'
}
Many thanks, Sanghamitra
I need to run a loop on a program which generates probabilities of 'spells' . The program inside the loop generates the probability of spells for one individual,and uses the bys command to generate the length of the spells and the the probabilities. I need to run a loop on it for a full dataset of individuals, where each individual has a person identification number (pid).
Below is my code. My understanding is that one cannot use the bys command in the loop, the program thus works very well without a loop, for one indiviudual, but doesn't within a loop. However, I need the bys command to be able to ask Stata to count the length of the spell for each group (hence the bys group).
Please can someone assist me with getting this code to run.
su pid, meanonly
forval i = 1/`r(max)' {
gen group= x != x[_n-1] if pid == `i'
replace group = sum(group) if pid == `i'
bys group: gen lengthspell =_N if pid == `i'
gen p_hat_spell = lengthspell/nyear if pid == `i'
}
Many thanks, Sanghamitra
Comment