[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: re: date and if function

From   Kit Baum <>
Subject   st: re: date and if function
Date   Wed, 15 Aug 2007 08:33:08 -0400

Ziad said

I want to calculate the number of days elapsed between date of
patients visits to the clinic, patients are expected to come each 4th
week, i.e. week4, week8, week12 etc..
I have date of visit (var date) and dummy variable for weeks, i.e.
week4 (1 or 0), week8 (1 or 0)

I am trying to generate new variable to calculate n of days between
week8 and week4, by saying
dayat8 = (date if week4==1) - (date if week8==1)

The last line does not vaguely resemble valid Stata syntax. But you could say something like

generate dayat8 = date * week4 - date * week8

I doubt if that's what you really want to do, though.

If you converted the data to the long form with -reshape- (as is often suggested on this list) you would end up with something like

patientid week date
111 4 06jun2007
111 8 05jul2007

etc. and you could then calculate what you want as

bysort patientid (week): gen interval = date - date[_n-1]

which would return missing for the first record and the number of days between those dates for the second. You could then 'reshape wide' to return the data to the wide form.

Computations such as these are usually easier to perform in the long form.


Kit Baum, Boston College Economics and DIW Berlin
An Introduction to Modern Econometrics Using Stata:

* For searches and help try:

© Copyright 1996–2023 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index