# Re: st: Date & if function

 From "Svend Juul" To Subject Re: st: Date & if function Date Wed, 15 Aug 2007 13:25:31 +0200

```Ziad wrote:

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) it does not work

----------------------------------------------------

This is not legal syntax:
generate dayat8 = (date if week4==1) - (date if week8==1)

I guess that your data are in long format, with one observation
for each patient and visit, i.e. that the dates for the week4 and
week8 visits are in different observations:

+--------------------------------------------+
| patid        date   week4   week8   week12 |
|--------------------------------------------|
1. |     1   10jan2007       1       0        0 |
2. |     1   07feb2007       0       1        0 |
3. |     1   07mar2007       0       0        1 |
+--------------------------------------------+

They must be brought together:

clear
input patid str9 sdate week4 week8 week12
1 10jan2007 1 0 0
1 07feb2007 0 1 0
1 07mar2007 0 0 1
end

// generate date = date(sdate,"dmy") // Stata 9 syntax
generate date = date(sdate,"DMY")    // Stata 10 syntax

// format date %d                    // Stata 9 syntax
format date %td                      // Stata 10 syntax

generate week=4 if week4==1
replace week=8 if week8==1
replace week=12 if week12==1
drop week4 week8 week12 sdate

reshape wide date , i(patid) j(week)
generate dayat8 = date8-date4

list
+----------------------------------------------------+
| patid       date4       date8      date12   dayat8 |
|----------------------------------------------------|
1. |     1   10jan2007   07feb2007   07mar2007       28 |
+----------------------------------------------------+

Hope this helps
Svend

__________________________________________

Svend Juul
Institut for Folkesundhed, Afdeling for Epidemiologi
(Institute of Public Health, Department of Epidemiology)
Vennelyst Boulevard 6
DK-8000  Aarhus C, Denmark
Phone: +45 8942 6090
Home:  +45 8693 7796
Email: sj@soci.au.dk
__________________________________________

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```