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

From |
Ilona Carneiro <ilonac@orange.es> |

To |
statalist@hsphsun2.harvard.edu |

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

Date |
Wed, 25 Feb 2009 23:06:14 +0100 |

id start end case tx 1 10 20 1 1 1 20 35 1 0 1 35 50 1 0 1 50 100 . .

What I need to get is the following: id start end case tx 1 10 20 1 1 1 40 50 1 0 1 50 100 . . I originally coded the following VERY crudely: /* To calculate the gaps */ sort id start

sort id start by id: drop if lagend[_n-1] > end & lagend[_n-1] < . & _n!=1 sort id start by id: drop if lagend[_n-1] > end & lagend[_n-1] < . & _n!=1 sort id start by id: drop if lagend[_n-1] > end & lagend[_n-1] < . & _n!=1 sort id start by id: drop if lagstart > end & lagstart < . & _n!=1 /* To update the start date */ sort id start by id: replace start = lagend[_n-1] if lagend[_n-1] < . & _n!=1 sort id start

id start end case tx 1 10 20 1 1 1 55 100 . .

Ilona On 25 Feb 2009, at 18:27, Martin Weiss wrote:

<>I was desperate to find an SJ tip for Ilona on the differencebetween "if"and "if"; turns out it is an FAQ: http://www.stata.com/support/faqs/lang/ifqualifier.html HTH Martin -----Ursprüngliche Nachricht----- Von: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Nick Cox Gesendet: Mittwoch, 25. Februar 2009 18:22 An: statalist@hsphsun2.harvard.edu Betreff: st: RE: Problem looping over spells for an individual Unless you are working under the aegis of -by:- _N will always be interpreted as the total number of observations. This code doesn't satisfy that. I echo Martin Weiss in suspecting that your -if `touse'- is a bug. You are almost certainly confusing the two flavours of -if-.Otherwise, your code still looks very confused and based on avariety ofmisunderstandings. Apart from `touse', which is defined by -marksample-,all of the local macros you refer to will be treated as empty strings,as none has been defined earlier in the program. I am surprised tohearthat it is running at all.It does not look as if you need a program anyway. My impression isthatall you need is to use -by:- but I don't understand your problem well enough to suggest better code. Someone else may be able to give betterhelp. If not, rather than a lengthy word description, you shouldperhapsgive an example of your data with the intended result. Nick n.j.cox@durham.ac.uk Ilona Carneiro I am trying to write a programme that will run a command sequentially for observations of an individual. For each individual I have multiple spells and multiple failures. However, the twist is that I also need to exclude a period of time at risk after each treatment (prophylaxis) and after each failure (to prevent double-counting of failures that may actually be the same episode). I managed to do this without any problem for the treatment, but if an episode is disqualified (by a prior treatment or episode) I don't want it to disqualify a subsequent episode. Therefore I need to run the code sequentially for each spell of an individual, but using the marksample touse code to run it "by" individual doesn't seem to be working - the "forvalues" seems to always interpret _N as the last observation in the whole dataset, not the last observation for each individual. I have the following code: program define byid, byable(recall, noheader) marksample touse sort `id' `start' if `touse' { forvalues i = 1(1)`=_N' { replace lagend = (`end' + `lag') if ((`tx' > 0 & `tx' < .) | (`case'0 & `case' < .))drop if lagend[`i'-1]>`end' & `id'[`i'-1]==`id' } } end gen lagend=. qui by id: byid but I get the error: 2nd by group not found r(111); And the programme isn't doing what I need it to. * * 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/ * * 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/

* * 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/

**Follow-Ups**:**RE: st: AW: RE: Problem looping over spells for an individual***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

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

**st: RE: Problem looping over spells for an individual***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

**st: AW: RE: Problem looping over spells for an individual***From:*"Martin Weiss" <martin.weiss1@gmx.de>

- Prev by Date:
**Re: st: "flat region resulting in a missing likelihood" (error message)** - Next by Date:
**st: ZINb with endogenous regressors** - Previous by thread:
**st: AW: RE: Problem looping over spells for an individual** - Next by thread:
**RE: st: AW: RE: Problem looping over spells for an individual** - Index(es):

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