Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

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

From |
Nick Cox <njcoxstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: While/if for individual observations |

Date |
Thu, 21 Mar 2013 12:07:16 +0000 |

I can't follow all of this, but I have some suggestions which should help. First off, a sufficient reason for your code to fail is that you cannot use subscripts in a -replace- statement, so that for example replace abxin28days[`i'] = 1 + abxin28days[`i'] would need to be replace abxin28days = 1 + abxin28days[`i'] in `i' except that the subscript is not needed and replace abxin28days = 1 + abxin28days in `i' would do fine. (Code like this suggests knowing a lot about other languages, which does not always help.) There is also some major confusion between local macros and variables, which are quite different beasts in Stata. Second, there are cases in which a loop over observations is needed, and SJ-7-3 pr0033 . . . . . . . . . . . . . . Stata tip 51: Events in intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox Q3/07 SJ 7(3):440--443 (no commands) tip for counting or summarizing irregularly spaced events in intervals Free .pdf at http://www.stata-journal.com/article.html?article=pr0033 discusses technique for problems a bit like yours. However, I am not clear that you actually need such an approach here (but the ideas may help in other problems). Third, there are two kinds of manipulation bound together here, summarising antibiotic use and the idea that a lapse of 28 days implies a fresh start. I would rather take the second first, and here advertise -panelthin- (SSC) as addressing it. Fourth, I don't get what your calculation is with the antibiotics, and don't see where numbers such as 4 and 6 come from. But using -egen- with -by()- options might be your answer. I have given you a -spell- variable. Code so far clear input ID str9 VisitDate Antibiotics_YN 100 01feb2011 0 100 02feb2011 0 100 04feb2011 1 100 10may2011 1 100 10oct2011 1 101 01feb2011 0 101 12mar2011 0 101 13mar2011 0 101 14mar2011 0 101 18mar2011 1 end gen visitdate = date(VisitDate, "DMY") format visitdate %td tsset ID visitdate capture ssc inst panelthin, replace panelthin, gen(start) min(28) bysort ID (visitdate) : gen spell = sum(start) l , sepby(ID) +--------------------------------------------------------+ | ID VisitDate Antibi~N visitdate start spell | |--------------------------------------------------------| 1. | 100 01feb2011 0 01feb2011 1 1 | 2. | 100 02feb2011 0 02feb2011 0 1 | 3. | 100 04feb2011 1 04feb2011 0 1 | 4. | 100 10may2011 1 10may2011 1 2 | 5. | 100 10oct2011 1 10oct2011 1 3 | |--------------------------------------------------------| 6. | 101 01feb2011 0 01feb2011 1 1 | 7. | 101 12mar2011 0 12mar2011 1 2 | 8. | 101 13mar2011 0 13mar2011 0 2 | 9. | 101 14mar2011 0 14mar2011 0 2 | 10. | 101 18mar2011 1 18mar2011 0 2 | +--------------------------------------------------------+ On Wed, Mar 20, 2013 at 7:10 PM, Williams, Barbara <Barbara.Williams@vmmc.org> wrote: > My data: > I have a list of 20,000+ patient visits to a clinic with the following 3 variables: > > ID VisitDate Antibiotics_YN > 100 01feb2011 0 > 100 02feb2011 0 > 100 04feb2011 1 > 100 10may2011 1 > 100 10oct2011 1 > 101 01feb2011 0 > 101 12mar2011 0 > 101 13mar2011 0 > 101 14mar2011 0 > 101 18mar2011 1 > ... > > My question: > I would like to create a new variable which labels Antibiotics WITHIN 28 days of their first visit. > AND If the patient has another visit more than 28 days after the first visit, the time is re-set (new series). > For ID=100 First series of visits beginning 01feb2011 Antibiotics on visitday = 4 > Second series of visits beginning 10may2011 Antibiotics on visitday = 1 > Third series of visits beginning 10oct2011 Antibiotics on visitday =1 > For ID=101 First series of visits beginning 01feb2011 No antibiotics > Second series of visits beginning 12mar2011 Antibiotics on visitday = 6 > > > My syntax (doesn't run, I'm not sure how to do this): > local i = 1 > local N = _N > while `i' <=`N' { > local firstdate = VisitDate[`i'] > while ID[`i'] == ID[`i' - 1] & VisitDate[`i'] <= firstdate + 28 { > replace abxin28days[`i'] = 1 + abxin28days[`i'] > local `i' = `i' + 1 } > replace firstdate[`i'] = VisitDate[`i'] if _n==`i' > } > * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**RE: st: While/if for individual observations***From:*"Williams, Barbara" <Barbara.Williams@vmmc.org>

**References**:**st: While/if for individual observations***From:*"Williams, Barbara" <Barbara.Williams@vmmc.org>

- Prev by Date:
**Re: st: calculating the whiskers on a boxplot using -twoway-** - Next by Date:
**st: Alternatives to box plots** - Previous by thread:
**st: While/if for individual observations** - Next by thread:
**RE: st: While/if for individual observations** - Index(es):