# Re: st: Date & if function

 From Ziad El-Khatib To statalist@hsphsun2.harvard.edu, SJ@soci.au.dk Subject Re: st: Date & if function Date Wed, 15 Aug 2007 06:27:33 -0700

```Thank you!
the last command
> reshape wide date , i(patid) j(weeks)
did not work because weeks contains missing values, any tips?

Thank you in advance and best regards

On 8/15/07, Svend Juul <SJ@soci.au.dk> 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/
>

--
AIDS Unit
National Institute for Communicable Diseases (NICD)
Private Bag X4, Sandringham, 2131
Johannesburg, South Africa
Mobile: +27 (0) 72-52 39 716
Phone: +27 (0) 11-386 6433
Fax (NICD): +27 (0)11 386 6453
Fax (alternative): +27 (0)866 18 2871
http://www.nhls.ac.za/div_nicd.html

PhD student
Division of International Health (IHCAR)
Karolinska Institutet
www.phs.ki.se/ihcar

*
*   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/
```