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

# Re: st: Difference in values within the same variable

 From Sara Muller To statalist@hsphsun2.harvard.edu Subject Re: st: Difference in values within the same variable Date Wed, 04 Jan 2012 08:28:53 +0000

Thank you for everyone's helpful comments. I can now identify the people I need in my analysis sample.
```
Very best wishes,
Sara

On 03/01/2012 14:28, Nick Cox wrote:
```
```A small extra flourish would be

bysort patid (timesince) : gen selected = timesince[1]<  183
edit if selected

where "183" or any similar value represents six months in days. That
is, if the smallest value of -timesince- is less than six months, then
that patient had two or more prescriptions in a period of six months
or less. Such patients have -selected- 1 and others have -selected- 0.

Nick

On Tue, Jan 3, 2012 at 2:06 PM, Christopher Baum<kit.baum@bc.edu>  wrote:
```
```<>
Sara said

I have long data with multiple prescriptions per person. I want to
define the length of time between successive prescriptions (see data
below, which is sorted by patid (person ID) and presc_date (date of
prescription)), with the eventual aim of including in my analysis only
people who received 2 or more prescription within 6 months of each other.

I'm not bothered really how I do this, although I have been trying to do
it by calculating another variable that gives the time from the previous
prescription (within patid). I believe the equivalent command in SPSS is
called a 'lag', but searching  for this in Stata doesn't see to have
turned up the right stuff.

--------------
clear all
input patid  str9  presc_date
4457    11aug1997
4457    18aug1997
4457    15sep1997
4457    29sep1997
4457    27oct1997
4457    15dec1997
4457    15dec1997
4457    16apr1998
4457    11may1998
end
g rxd = date(presc_date,"DMY")
format rxd %td
bysort patid (rxd): g timesince = rxd[_n] - rxd[_n - 1]
list
-----------------
```
```*
*   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/
```