Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: While/if for individual observations


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: While/if for individual observations
Date   Thu, 21 Mar 2013 17:39:03 +0000

Thanks for the THANKS!

1. My code is fine. I could not have cited its results otherwise. The line

l, sepby(ID)

is a separate command. -l- means -list-.

I guess the problem is merely that the lines got combined in your mailer.

2. This is easy. After

bysort ID (visitdate) : gen spell = sum(start)

you can go

bysort ID spell (visitdate) : gen elapsed = visitdate - visitdate[1]

If this is cryptic, a  reading list includes

a. The help for -by:-, which may be too concise as a tutorial.

b.

SJ-2-1  pr0004  . . . . . . . . . . Speaking Stata:  How to move step by: step
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q1/02   SJ 2(1):86--102                                  (no commands)
        explains the use of the by varlist : construct to tackle
        a variety of problems with group structure, ranging from
        simple calculations for each of several groups to more
        advanced manipulations that use the built-in _n and _N

.pdf at  http://www.stata-journal.com/sjpdf.html?articlenum=pr0004

which may be too long-winded.

c. -tsspell- (SSC) and its help file, because it looks as if you will
be working with spells in the future.

d.

SJ-7-2  dm0029  . . . . . . . . . . . . . . Speaking Stata: Identifying spells
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q2/07   SJ 7(2):249--265                                 (no commands)
        shows how to handle spells with complete control over
        spell specification

.pdf at http://www.stata-journal.com/sjpdf.html?articlenum=dm0029

for the same reason.

On Thu, Mar 21, 2013 at 4:49 PM, Williams, Barbara
<[email protected]> wrote:
> Nick-
> THANK YOU! For your reply to my question.
> (I needed to think about my problem in a new way)
> This is what I need ALMOST.
>
> 2 things:
> 1.   The code that you sent does not work-- invalid "l" (I have Stata 12):
> . gen visitdate = date(VisitDate, "DMY")
> . format visitdate %td
> . tsset ID visitdate
>        panel variable:  ID (weakly balanced)
>         time variable:  visitdate, 01feb2011 to 10oct2011, but with gaps
>                 delta:  1 day
> . capture ssc inst panelthin, replace
> . panelthin, gen(start) min(28)
> . bysort ID (visitdate) : gen spell = sum(start) l, sepby(ID)
> invalid 'l'
> r(198);
>
> 2. For each spell, I want number of elapsed days since the "start":
> generate elapsedays = 0 if start ==1
> replace  elapsedays = X  if ???
>
> Barb Williams
>
> -----Original Message-----
> From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
> Sent: Thursday, March 21, 2013 5:07
> To: [email protected]
> Subject: Re: st: While/if for individual observations
>
> 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 <[email protected]> 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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index