Statalist


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

Re: st: add up the total stay


From   Arun Rajamohan <[email protected]>
To   [email protected]
Subject   Re: st: add up the total stay
Date   Fri, 12 Oct 2007 07:03:21 -0400

Hi,

Oh yes having the dataset in stata helps. I have pasted the log here that probably shows how to manipulate your data.

/*beginning of the log - I have your sample data stored as stay.dta on my desktop
. use ~/desktop/stay.dta

. list

+-------------------------------+
| id datecame datewent |
|-------------------------------|
1. | SD101 05-Nov-05 05-Nov-05 |
2. | SD101 05-Nov-05 11-Nov-05 |
3. | SD101 28-Apr-05 28-Apr-05 |
4. | SD105 03-Jul-06 03-Jul-06 |
5. | SD105 29-Mar-06 29-Mar-06 |
|-------------------------------|
6. | SD105 13-Jan-06 14-Jan-06 |
7. | SD105 10-Jan-06 13-Jan-06 |
8. | SD105 24-May-04 25-May-04 |
9. | SD217 22-Sep-05 22-Sep-05 |
+-------------------------------+

. gen datein=date(datecame, "DM20Y")

/* if you use stata9 or less "DM20Y" in the commands above and below can be in lowercase

. gen dateout=date(datewent, "DM20Y")

/* by using format datein %td you can convert the resulting 5 digit number (like 16745) to normal dates (like 05Nov2005)

. list

+--------------------------------------------------+
| id datecame datewent datein dateout |
|--------------------------------------------------|
1. | SD101 05-Nov-05 05-Nov-05 16745 16745 |
2. | SD101 05-Nov-05 11-Nov-05 16745 16751 |
3. | SD101 28-Apr-05 28-Apr-05 16554 16554 |
4. | SD105 03-Jul-06 03-Jul-06 16985 16985 |
5. | SD105 29-Mar-06 29-Mar-06 16889 16889 |
|--------------------------------------------------|
6. | SD105 13-Jan-06 14-Jan-06 16814 16815 |
7. | SD105 10-Jan-06 13-Jan-06 16811 16814 |
8. | SD105 24-May-04 25-May-04 16215 16216 |
9. | SD217 22-Sep-05 22-Sep-05 16701 16701 |
+--------------------------------------------------+

. gen stay_duration=dateout-datein

. replace stay_duration=0.5 if stay_duration==0
(5 real changes made)

. list

+-------------------------------------------------------------+
| id datecame datewent datein dateout stay_d~n |
|-------------------------------------------------------------|
1. | SD101 05-Nov-05 05-Nov-05 16745 16745 .5 |
2. | SD101 05-Nov-05 11-Nov-05 16745 16751 6 |
3. | SD101 28-Apr-05 28-Apr-05 16554 16554 .5 |
4. | SD105 03-Jul-06 03-Jul-06 16985 16985 .5 |
5. | SD105 29-Mar-06 29-Mar-06 16889 16889 .5 |
|-------------------------------------------------------------|
6. | SD105 13-Jan-06 14-Jan-06 16814 16815 1 |
7. | SD105 10-Jan-06 13-Jan-06 16811 16814 3 |
8. | SD105 24-May-04 25-May-04 16215 16216 1 |
9. | SD217 22-Sep-05 22-Sep-05 16701 16701 .5 |
+-------------------------------------------------------------+

.
Hope it works for you,
-A

On 12-Oct-07, at 5:04 AM, STATA Dndee wrote:


Hi Arun Rajamohan

Thanks for your reply.
The database is in STATA format and I had better to
should manage everything there. Total number of
records is so high that I would prefer to work with
STATA and not to enter them in Excel.
ID	DateCame	DateWent	DateWent-DateCame
TotalStayEachVisit
SD101	05-Nov-05	05-Nov-05	0.5	_
SD101	05-Nov-05	11-Nov-05	6	6.5
SD101	28-Apr-05	28-Apr-05	0.5	0.5
SD105	03-Jul-06	03-Jul-06	0.5	0.5
SD105	29-Mar-06	29-Mar-06	0.5	0.1
SD105	13-Jan-06	14-Jan-06	1	_
SD105	10-Jan-06	13-Jan-06	3	4
SD105	24-May-04	25-May-04	1	1
SD217	22-Sep-05	22-Sep-05	0.5	0.5

What I need is calculation of TotalStayEachVisit
which depends on 1) ID and 2) number of visits. As can
be seen in sample some visitors came only to one
department and some to more than one. I need syntaxes
in STATA.
There is no problem with formatting for dates.
Regards




--- Arun Rajamohan <[email protected]> wrote:

Hi,

I am not sure what you mean by database. Hopefully
it is convertible
to excel or raw or csv file. I draged the sample
data from your email
and saved it as a raw file. Then I loaded it into
stata using infix. T

Your sample dataset is,

SD101 05-Nov-05 05-Nov-05
SD101 05-Nov-05 11-Nov-05
SD101 28-Apr-05 28-Apr-05
SD105 03-Jul-06 03-Jul-06
SD105 29-Mar-06 29-Mar-06
SD105 13-Jan-06 14-Jan-06
SD105 10-Jan-06 13-Jan-06
SD105 24-May-04 25-May-04
SD217 22-Sep-05 22-Sep-05

I  sucked it up into stata  using infix ...

infix str id 1-5 str d1 7-8 str m1 10-12 y1 14-15
str d2 17-18 str m2
20-22 y2 24-25 using ~/desktop/stay.raw, clear

Now you will have your data in stata looking like
this...

id d1 m1 y1 d2 m2 y2
SD101 05 Nov 05 05 Nov 05
...

Then you may have to add 2000 to the year values.

[I would appreciate if someone can update me on an
alternate method.
Some of my own datasets have years in the format 96,
97, 01, 04 etc.
instead of 1996, 1997, 2001, 2004... Stata date
system does not like
years in 2 digit format. Am I right? I've always had
to use a do file
to sort through the years and convert them.]


Then I recombine the date values using
  generate came=d1+m1+y1
  generate went=d2+m2+y2
to make it look like this,


      +-------------------------------+
      |    id        came        went |
      |-------------------------------|
   1. | SD101   05Nov2005   05Nov2005 |
   2. | SD101   05Nov2005   11Nov2005 |
   3. | SD101   28Apr2005   28Apr2005 |
   4. | SD105   03Jul2006   03Jul2006 |
   5. | SD105   29Mar2006   29Mar2006 |
      |-------------------------------|
   6. | SD105   13Jan2006   14Jan2006 |
   7. | SD105   10Jan2006   13Jan2006 |
   8. | SD105   24May2004   25May2004 |
      +-------------------------------+

Now if you format your variables 'came' and 'went'
into date
variables by using,
gen chkin = date(came,"DMY")
gen chkout = date(went,"DMY")
  you should get,


+------------------------------------------------+
      |    id        came        went   chkin
chkout |

|------------------------------------------------|
   1. | SD101   05Nov2005   05Nov2005   16745
16745 |
   2. | SD101   05Nov2005   11Nov2005   16745
16751 |
   3. | SD101   28Apr2005   28Apr2005   16554
16554 |
   4. | SD105   03Jul2006   03Jul2006   16985
16985 |
   5. | SD105   29Mar2006   29Mar2006   16889
16889 |

|------------------------------------------------|
   6. | SD105   13Jan2006   14Jan2006   16814
16815 |
   7. | SD105   10Jan2006   13Jan2006   16811
16814 |
   8. | SD105   24May2004   25May2004   16215
16216 |

+------------------------------------------------+

Now chkin and chkout are actually number of days
from 1 Jan 1960.
Read more about this at,
http://www.ats.ucla.edu/stat/stata/modules/dates.htm

Now using generate, you should be able to subtract
chkin and chkout
and obtain the stay duration.

To convert a 0 duration to 0.5,

replace dur=0.5 if dur==0

Hope it helps

-A

On 11-Oct-07, at 3:33 PM, STATA Dndee wrote:

Dear all
I have a database form visitors stayed in one or
more
departments during each visit.  I want to
calculate
total length of stay during each visit. I need to
calculate something like those I calculated
manually
under columns “Date came-Date went” and “total
stay
each visit”. If Date came= Date went, then the
stay
would be 0.5 and not zero.
Your help is really appreciated.
All the bests

ID DateCame DateWent DateCame-DateWent
TotalStayEachVisit
SD101 05-Nov-05 05-Nov-05 0.5 _
SD101 05-Nov-05 11-Nov-05 6 6.5
SD101 28-Apr-05 28-Apr-05 0.5 0.5
SD105 03-Jul-06 03-Jul-06 0.5 0.5
SD105 29-Mar-06 29-Mar-06 0.5 0.1
SD105 13-Jan-06 14-Jan-06 1 _
SD105 10-Jan-06 13-Jan-06 3 4
SD105 24-May-04 25-May-04 1 1
SD217 22-Sep-05 22-Sep-05 0.5 0.5






___________________________________________________________
Want ideas for reducing your carbon footprint?
Visit Yahoo! For
Good

http://uk.promotions.yahoo.com/forgood/environment.html
*
*   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/
Arun Rajamohan
Department of Biology
Room 360, Biological & Geological Sciences Building
1151 Richmond Street North
The University of Western Ontario
London, ON N6A 5B7 CANADA

Office: + 1-519-661-2111 ext 80582
Office Fax: + 1-519-661-3935


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



___________________________________________________________
Yahoo! Answers - Got a question? Someone out there knows the answer. Try it
now.
http://uk.answers.yahoo.com/
*
* 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/
Arun Rajamohan
Department of Biology
Room 360, Biological & Geological Sciences Building
1151 Richmond Street North
The University of Western Ontario
London, ON N6A 5B7 CANADA

Office: + 1-519-661-2111 ext 80582
Office Fax: + 1-519-661-3935




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



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