[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
RE: AW: st: AW: RE: Problem looping over spells for an individual |

Date |
Sun, 1 Mar 2009 18:22:21 -0000 |

Not so. The bug is in your debugging line, not the surrounding code. Consider - noi di "t=" `t' `t' calls up a variable name, which in this example is __000004. You don't say which observation you want to look at. Neither Stata in general nor -display- in particular will fill in for your lack of information, either with the current loop index or with anything else. By default -display- just defaults to showing the value for the first observation, here `t'[1], every time around the loop. What I think you intended was . noi di "t=" `t'[`i'] -- which will, I predict, produce a display of 1, 2, etc. The point can be seen independently of any programming context. Call up the auto data and experiment sysuse auto di mpg forval i = 1/74 { di mpg } -display- is for display of scalars; given a variable name, it just shows the value of that variable in observation 1, regardless of context. If you want to describe this behaviour as perverse, I think you have a good case, but regardless of that you did not ask for what you wanted, so it is no surprise that Stata didn't comply. (NB please trim your messages of past material. Digest readers have to plough through a lot of gunk otherwise.) Nick n.j.cox@durham.ac.uk Ilona Carneiro Thanks Martin Here is a sample of the data that requires this fix: pin start end case tx 1 10 20 1 1 1 20 35 1 0 1 35 50 1 0 1 50 100 . and here is an extract of the trace from the programme which is 'byable' and I'm running "by pin:" which is the personal identification number. This shows that the programme should be generating a variable that is equal to the observation number _n within pin sorted by the observation start date. It calculates the _N correctly for each pin, but as you can see from the "noi di "t=" `t', the tempvar `t' stays constant at 1 when `i' =2 showing that it has moved to the subsequent observation of the dataset. I would prefer not to make the programme byable, as it actually needs to be nested within a bigger programme, but I need a way to run the code sequentially. - tempvar T t - sort `_byvars' start = sort pin start - by `_byvars': gen `t' = _n = by pin: gen __000004 = _n - sort `_byvars' start = sort pin start - by `_byvars': gen `T' = _N * `touse' = by pin: gen __000003 = _N * __000002 - sort `_byvars' start = sort pin start - sum `T', meanonly = sum __000003, meanonly - local tmax = r(max) - drop `T' = drop __000003 - replace lagend = (end + 19 + 1) if (anmal > 0 & anmal < .) - sort `_byvars' start = sort pin start - forvalues i = 1(1)`tmax' { = forvalues i = 1(1)2 { - noi di "T=" `tmax' = noi di "T=" 2 T=2 - noi di "t=" `t' = noi di "t=" __000004 t=1 - noi di "i=" `i' = noi di "i=" 1 i=1 - drop if end < lagend[`i'-1] & lagend[`i'-1] < . & `t'==`i' & `i'!=1 = drop if end < lagend[1-1] & lagend[1-1] < . & __000004==1 & 1!=1 - replace lagend = (end + 19 + 1) if (mal0 > 0 & mal0 < .) & lagend==. & `t'==`i' & `i'<`tmax' = replace lagend = (end + 19 + 1) if (mal0 > 0 & mal0 < .) & lagend==. & __000004==1 & 1<2 - } - noi di "T=" `tmax' = noi di "T=" 2 T=2 - noi di "t=" `t' = noi di "t=" __000004 t=1 - noi di "i=" `i' = noi di "i=" 2 i=2 - drop if end < lagend[`i'-1] & lagend[`i'-1] < . & `t'==`i' & `i'!=1 = drop if end < lagend[2-1] & lagend[2-1] < . & __000004==2 & 2!=1 - replace lagend = (end + 21 + 1) if (mal0 > 0 & mal0 < .) & lagend==. & `t'==`i' & `i'<`tmax' = replace lagend = (end + 21 + 1) if (mal0 > 0 & mal0 < .) & lagend==. & __000004==2 & 2<2 - } - } * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**References**:**Re: AW: st: AW: RE: Problem looping over spells for an individual***From:*Ilona Carneiro <ilonac@orange.es>

- Prev by Date:
**RE: st: AW: RE: Problem looping over spells for an individual** - Next by Date:
**st: how to reconstruct a minimum acceptable income from a set of binary variables?** - Previous by thread:
**Re: AW: st: AW: RE: Problem looping over spells for an individual** - Next by thread:
**RE: st: Re: controlling output in the results window** - Index(es):

© Copyright 1996–2015 StataCorp LP | Terms of use | Privacy | Contact us | What's new | Site index |