Bookmark and Share

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

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

Re: st: Difference in values within the same variable

From   Sara Muller <>
Subject   Re: st: Difference in values within the same variable
Date   Tue, 03 Jan 2012 14:20:23 +0000

Thank you - that works perfectly.

Dr Sara Muller
National School for Primary Care Research Fellow
Arthritis UK Primary Care Centre
Primary Care Sciences
Keele University

Tel 01782 734842
Fax 01782 733911

On 03/01/2012 14:06, Christopher Baum 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
g rxd = date(presc_date,"DMY")
format rxd %td
bysort patid (rxd): g timesince = rxd[_n] - rxd[_n - 1]

Kit Baum   |   Boston College Economics&  DIW Berlin   |
                              An Introduction to Stata Programming  |
   An Introduction to Modern Econometrics Using Stata  |

*   For searches and help try:
*   For searches and help try:

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