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

# Re: st: Event within dates

 From David Kantor To statalist@hsphsun2.harvard.edu Subject Re: st: Event within dates Date Thu, 01 Dec 2011 16:27:33 -0500

```Oops, I really meant...

```
```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'
```        }
}
```
```
Note the reference to drug[`j'] ==2.
But again, there is probably a better way.
----

At 03:51 PM 12/1/2011, I wrote:
```
The following might work. It is an attempt to fill in some features that were needed in your method. But it is somewhat brute-force in its approach. It does looping over observations (doubly), which is rarely needed. There is probably a much better way to do this (involving -by-, for one), but this is all I can do at the moment.
```HTH
--David

forval i = 1/ `N' {
forval j = 1/ `N' {
```
replace rs1=1 if drug[`i]==1 & id[`i']==id[`j'] & inrange(date[`i'] - date[`j'],-56,56) in `i'
```        }
}

At 02:15 PM 12/1/2011, David Kpento 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). Could somebody please help me with any code? I am new to Stata so have struggled with this for days.
```

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.
```
gen date1 = date(date, "dmy")
format date1 %d
drop date
rename date1 date

g rs1=0
local N=_N
forval i = 1/ `N' {
replace rs1=1 if drug==1 & id[`i'] & inrange(date[`i'] - date,-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/
```
```
*
*   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/
```