# RE: st: AW: Creating number of days per month

 From "Nick Cox" To Subject RE: st: AW: Creating number of days per month Date Mon, 2 Nov 2009 12:24:25 -0000

```Martin gave you a very good start. Meanwhile, I see no clue of what you tried by way of solution.

Given variables -discha- -admi- -id-, more or less as in Martin's example, consider:

gen duration = discha - admi + 1
assert duration > 0
expand duration
bysort id : gen month = mofd(admi[1] + _n - 1)
format month %tmM_CY
contract admi discha duration id month
reshape wide _freq , i(id)  j(month)

I used -id- where Martin used -patientid-.

Nick
n.j.cox@durham.ac.uk

Rajaram Subramanian Potty

Martin thanks for the quick reply and trying the query. I think, I
might not have provided my query correctly. I will be giving example
with the first observation. the patient was admitted in 30nov2007
and discharged in 14feb2008. Now I want to know for this person in
each indiviudal month and individual year how many days he was in the
hospital. For this case Nov 2007 he was there in 1 day, December  2007
31 days, January 2008 31 days, February 2008 14 days. For indicating
this 4 variables has to be generated such as nov2007, Dec2007,
Jan2008, Febr2008. Number of variables may change according to the
date of discharge and date of admission. Hope that now you may be
clear with my query.

On Mon, Nov 2, 2009 at 4:00 PM, Martin Weiss <martin.weiss1@gmx.de> wrote:

> Here is part of the solution, how to get the days in the month of admission
> and discharge. Not sure whether there is an easy way to fill in the months
> in between with their corresponding number of days...
>
> *************
> clear*
>
> inp str10(var1 var2)
> "30nov2007"   "14feb2008"
> "21jan2009"   "07apr2009"
> "24apr2008"   "09jul2008"
> "22feb2008"   "08may2008"
> "01oct2008"   "16dec2008"
> "13jul2009"   "28sep2009"
> "04oct2008"   "20dec2008"
> "07jul2009"   "22sep2009"
> "03jun2008"   "19aug2008"
> "11feb2009"   "30apr2009"
> "31mar2007"   "17jun2007"
> "01may2007"   "19jul2007"
> "04apr2007"   "22jun2007"
> "03jul2008"   "20sep2008"
> "10jul2007"   "27sep2007"
> "10jul2009"   "28sep2009"
> "08aug2007"   "27oct2007"
> "15jun2009"   "03sep2009"
> "12oct2007"   "31dec2007"
> "20dec2008"   "11mar2009"
> "04aug2009"   "24oct2009"
> "01nov2008"   "21jan2009"
> "04dec2008"   "23feb2009"
> "31jul2009"   "21oct2009"
> "03feb2009"   "26apr2009"
> "24jul2007"   "14oct2007"
> "16apr2009"   "07jul2009"
> "23nov2007"   "14feb2008"
> "15apr2008"   "07jul2008"
> "01oct2008"   "23dec2008"
> "30sep2008"   "23dec2008"
> "07jul2007"   "29sep2007"
> "14oct2008"   "07jan2009"
> "15apr2009"   "09jul2009"
> "02nov2007"   "27jan2008"
> "30apr2009"   "26jul2009"
> "15apr2008"   "11jul2008"
> "27mar2009"   "23jun2009"
> "01apr2007"   "29jun2007"
> "07jan2008"   "05apr2008"
> "09apr2009"   "07jul2009"
> "21feb2009"   "23may2009"
> "03dec2008"   "04mar2009"
> "02jun2009"   "02sep2009"
> "01apr2009"   "03jul2009"
> "01apr2009"   "03jul2009"
> "17apr2008"   "20jul2008"
> "05may2007"   "08aug2007"
> "19dec2007"   "27mar2008"
> "26jun2007"   "04oct2007"
> "29nov2008"   "09mar2009"
> "28jan2009"   "08may2009"
> "01oct2007"   "11jan2008"
> "01jul2009"   "13oct2009"
> "25dec2007"   "08apr2008"
> "30apr2007"   "14aug2007"
> "19jun2007"   "03oct2007"
> "01apr2007"   "16jul2007"
> "19mar2008"   "07jul2008"
> "07sep2007"   "26dec2007"
> "09jan2008"   "30apr2008"
> "10nov2008"   "02mar2009"
> "09jan2008"   "30apr2008"
> "12may2008"   "03sep2008"
> "06jun2009"   "30sep2009"
> "06jan2009"   "02may2009"
> "02apr2008"   "28jul2008"
> "19may2008"   "15sep2008"
> "21aug2008"   "19dec2008"
> "21nov2007"   "24mar2008"
> "04apr2009"   "08aug2009"
> "19jun2007"   "24oct2007"
> "15may2009"   "26sep2009"
> "13nov2008"   "29mar2009"
> "20aug2007"   "13jan2008"
> "14oct2008"   "10mar2009"
> "29nov2008"   "25apr2009"
> "18sep2007"   "14feb2008"
> "30apr2009"   "26sep2009"
> "10apr2007"   "08sep2007"
> "05apr2008"   "03sep2008"
> "05mar2008"   "08aug2008"
> "21apr2009"   "29sep2009"
> "26feb2009"   "08aug2009"
> "03feb2009"   "19jul2009"
> "28apr2007"   "18oct2007"
> "04apr2007"   "28sep2007"
> "29apr2007"   "25oct2007"
> "09jan2008"   "09jul2008"
> "07oct2007"   "08apr2008"
> "20feb2009"   "07sep2009"
> "12may2008"   "07dec2008"
> "04may2007"   "18dec2007"
> "05may2007"   "06jan2008"
> "26jun2007"   "16mar2008"
> "22may2007"   "16feb2008"
> "10apr2007"   "11jan2008"
> "24feb2007"   "19dec2007"
> "02may2007"   "04mar2008"
> "28apr2007"   "10mar2008"
> "17apr2007"   "08mar2008"
> end
>
> gen discha=date(var2, "DMY")
> drop var1 var2
> gen patientid=_n
> expand 2
> bys patientid:  /*
> */ if _n==1
> bys patientid:  /*
> */ replace days=day(discha)  /*
> */ if _n==_N
>
> compress
> list, noobs sepby(patientid)
> *************

Rajaram Subramanian Potty
>
> On Mon, Nov 2, 2009 at 10:33 AM, Rajaram Subramanian Potty
> <rajara999@gmail.com> wrote:
>
>> I have the following data indciating the date of visit and date of
>> discharge of patients. Now, I want to consturct variables to indicate
>> how may days the patient was there in the hospital or centre in a
>> month in a particular year. Some patients might have stayed in the
>> hospital ever for 2 years. The following is the 101 observations from
>> the data set. Var1 is the date of admission and var2 is the date of
>> discharge.
>>
>>
>>                var1               var2
>> 24400.   30nov2007   14feb2008
>> 24401.   21jan2009   07apr2009
>> 24402.   24apr2008   09jul2008
>> 24403.   22feb2008   08may2008
>> 24404.   01oct2008   16dec2008
>> 24405.   13jul2009   28sep2009
>> 24406.   04oct2008   20dec2008
>> 24407.   07jul2009   22sep2009
>> 24408.   03jun2008   19aug2008
>> 24409.   11feb2009   30apr2009
>> 24410.   31mar2007   17jun2007
>> 24411.   01may2007   19jul2007
>> 24412.   04apr2007   22jun2007
>> 24413.   03jul2008   20sep2008
>> 24414.   10jul2007   27sep2007
>> 24415.   10jul2009   28sep2009
>> 24416.   08aug2007   27oct2007
>> 24417.   15jun2009   03sep2009
>> 24418.   12oct2007   31dec2007
>> 24419.   20dec2008   11mar2009
>> 24420.   04aug2009   24oct2009
>> 24421.   01nov2008   21jan2009
>> 24422.   04dec2008   23feb2009
>> 24423.   31jul2009   21oct2009
>> 24424.   03feb2009   26apr2009
>> 24425.   24jul2007   14oct2007
>> 24426.   16apr2009   07jul2009
>> 24427.   23nov2007   14feb2008
>> 24428.   15apr2008   07jul2008
>> 24429.   01oct2008   23dec2008
>> 24430.   30sep2008   23dec2008
>> 24431.   07jul2007   29sep2007
>> 24432.   14oct2008   07jan2009
>> 24433.   15apr2009   09jul2009
>> 24434.   02nov2007   27jan2008
>> 24435.   30apr2009   26jul2009
>> 24436.   15apr2008   11jul2008
>> 24437.   27mar2009   23jun2009
>> 24438.   01apr2007   29jun2007
>> 24439.   07jan2008   05apr2008
>> 24440.   09apr2009   07jul2009
>> 24441.   21feb2009   23may2009
>> 24442.   03dec2008   04mar2009
>> 24443.   02jun2009   02sep2009
>> 24444.   01apr2009   03jul2009
>> 24445.   01apr2009   03jul2009
>> 24446.   17apr2008   20jul2008
>> 24447.   05may2007   08aug2007
>> 24448.   19dec2007   27mar2008
>> 24449.   26jun2007   04oct2007
>> 24450.   29nov2008   09mar2009
>> 24451.   28jan2009   08may2009
>> 24452.   01oct2007   11jan2008
>> 24453.   01jul2009   13oct2009
>> 24454.   25dec2007   08apr2008
>> 24455.   30apr2007   14aug2007
>> 24456.   19jun2007   03oct2007
>> 24457.   01apr2007   16jul2007
>> 24458.   19mar2008   07jul2008
>> 24459.   07sep2007   26dec2007
>> 24460.   09jan2008   30apr2008
>> 24461.   10nov2008   02mar2009
>> 24462.   09jan2008   30apr2008
>> 24463.   12may2008   03sep2008
>> 24464.   06jun2009   30sep2009
>> 24465.   06jan2009   02may2009
>> 24466.   02apr2008   28jul2008
>> 24467.   19may2008   15sep2008
>> 24468.   21aug2008   19dec2008
>> 24469.   21nov2007   24mar2008
>> 24470.   04apr2009   08aug2009
>> 24471.   19jun2007   24oct2007
>> 24472.   15may2009   26sep2009
>> 24473.   13nov2008   29mar2009
>> 24474.   20aug2007   13jan2008
>> 24475.   14oct2008   10mar2009
>> 24476.   29nov2008   25apr2009
>> 24477.   18sep2007   14feb2008
>> 24478.   30apr2009   26sep2009
>> 24479.   10apr2007   08sep2007
>> 24480.   05apr2008   03sep2008
>> 24481.   05mar2008   08aug2008
>> 24482.   21apr2009   29sep2009
>> 24483.   26feb2009   08aug2009
>> 24484.   03feb2009   19jul2009
>> 24485.   28apr2007   18oct2007
>> 24486.   04apr2007   28sep2007
>> 24487.   29apr2007   25oct2007
>> 24488.   09jan2008   09jul2008
>> 24489.   07oct2007   08apr2008
>> 24490.   20feb2009   07sep2009
>> 24491.   12may2008   07dec2008
>> 24492.   04may2007   18dec2007
>> 24493.   05may2007   06jan2008
>> 24494.   26jun2007   16mar2008
>> 24495.   22may2007   16feb2008
>> 24496.   10apr2007   11jan2008
>> 24497.   24feb2007   19dec2007
>> 24498.   02may2007   04mar2008
>> 24499.   28apr2007   10mar2008
>> 24500.   17apr2007   08mar2008
>>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```