Bookmark and Share

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]

Re: st: Fw: Event within dates


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Fw: Event within dates
Date   Mon, 5 Dec 2011 00:52:22 +0000

I have comments on various levels.

1. You posted this question before but only replied to some of David
Kantor's suggestions and questions. Repeating a question while
ignoring advice will often lead to experienced members just deleting a
posting.

2. The code in this latest posting will not work even in Stata 9 as
you omit crucial code converting the string date variable to a numeric
variable. This was given in your earlier post. (The syntax for
-date()- changed in Stata 10, but that does not affect the code you
should use.)

gen date1 = date(date, "dmy")
format date1 %d
drop date
rename date1 date

3. Your example appears to be wrong too as for -id- 3 the indicator
variable will never be 1 as 7 January and 22 March are more than 56
days apart.

4. The following code appears to do what you want.

g rs1=0

qui forval i = 1/`=_N' {
     count if id == id[`i'] & drug[`i'] == 2 & drug == 1 &
inrange(date[`i'] - date, -56,56)
     replace rs1 = (r(N) > 0) in `i'
}

5. The logic was explained in

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

which is accessible to you at
http://www.stata-journal.com/sjpdf.html?articlenum=pr0033

6. As a user of Stata 9, you should be able to -update- to 9.2. See
http://www.stata.com/support/faqs/res/history.html

Nick

On Sat, Dec 3, 2011 at 5:43 PM, David Kpento <dkpento@yahoo.com> wrote:
>
> I have prescription data in panel format for analysis. I wish to generate an indicator rs to be 1 for any subject who took drug2 within 56 days of taking drug1 (either before or after) as shown in sample data. I have done a code below but it generates rs to be 0. I have look at some examples on the Stata list without success. Please user somebody should have a look at the code for me. This is for my PhD.
>
>
> I use Stata/SE 9.0 for Windows.
>
> Many thanks
>
> David
> clear
> input id str9 date drug rs
> 1 08apr2009 1 0
> 1 08may2009 1 0
> 1 08jun2009 2 1
> 2 02jan2009 1 0
> 2 28jan2009 2 1
> 2 05apr2009 1 0
> 3 07jan2009 2 1
> 3 22mar2009 1 0
> 3 02jul2009 1 0
> end
>
> I have a code like this after formatting the date into a Stata date but rs1 is all zeros.
>
> g rs1=0
>
> forval i = 1/`=_N' {
>         forval j = 1/`=_N' {
>             replace rs1=1 if drug[`i']==1 & id[`i']==id[`j'] & ///
>        drug[`j'] ==2 & inrange(date[`i'] - date[`j'], -56,56) in `i'
>         }
> }
>
> }

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


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index