Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: RE: dates

 From Nick Cox <[email protected]> To [email protected] Subject Re: st: RE: dates Date Fri, 7 Oct 2011 00:33:17 +0100

```Suppose -visdate- as here is a daily date variable.

Then the length of the current month is given by the last day of the
current month, which is given by the first day of the next month less
1.

day(dofm(1 + mofd(visdate)) - 1)

In steps:

1. current month is mofd(visdate)
2. next month is 1 + mofd(visdate)
3. first day of next month is dofm(1 + mofd(visdate))
4. last day of this month is dofm(1 + mofd(visdate)) - 1
5. day of last day ... you got it long since.

But I never remember most of the function names and always have to
look them up.

It's key _never_ to type in rules about 28/31 or leap years, because

Nick

On Thu, Oct 6, 2011 at 11:55 PM, Steven Samuels <[email protected]> wrote:
> Oops! The original algorithm assigned days only from 1 to 15. The correction is below.  A better version  would assign days according to whether the month has 28, 29, 30, or 31 days, but I'll leave that to others.
>
>
> Steve
>
>
>
> With enough missing dates  it might be better to randomly assign a day of the month, or you risk distorting the distribution of inter-visit intervals.
>
>
>
>
> *********************************
> clear
> input str10 date
> 200801
> 20080113
> end
> set seed 21932
> gen visdate = date(date, "YMD")
> tempvar day
> gen str2 `day' = string(ceil(30*runiform())) if length(date)==6
> replace `day' = "0"+`day' if real(`day')<10
> gen fakeday = (length(date)==6)
> replace visdate = date(date + `day', "YMD") if length(date)==6
> format visdate %td
> list date visdate fakeday
> *****************************
>
>
>
> On Oct 6, 2011, at 5:46 PM, Michael Eisenberg wrote:
>
> Thanks so much.
>
> On Thu, Oct 6, 2011 at 8:23 AM, Nick Cox <[email protected]> wrote:
>> You don't say what "without success" means precisely.
>>
>> "200801" does not match either date pattern. If there is no information on day of month, Stata can only return missing for a daily date.
>>
>> -date("200801" + "15", "YMD")- seems to be the most common fudge. I would always tag such guessed dates with an indicator variable.
>>
>> Nick
>> [email protected]
>>
>> Michael Eisenberg
>>
>> I have a list of visit dates for patients.  Unfortunately, the format
>> is not constant.
>>
>> Most are listed with the year, month, day such as 20080105 for Jan 5,
>> 2008 but some are listed only with the year and month 200801 for Jan
>> 2008.
>>
>> I attempted to convert them into stata dates with the commands below
>> without success.
>>
>> gen ndate = date(dx_date, "YMD")
>> or
>> gen ndate = date(dx_date, "CCYYNNDD")
>>
>> Can stata handle such inconsistent data?
>>
>>
>> *
>> *   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/
>>
>
> *
> *   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/
>
>
> *
> *   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/
>

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