Bookmark and Share

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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: how does one automatically calculate the age in years by subtracting the system date from the date of birth?


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: how does one automatically calculate the age in years by subtracting the system date from the date of birth?
Date   Mon, 11 Mar 2013 10:58:24 +0000

For age calculations, see also -personage- (SSC).

As often explained on this list, format in the sense of Stata's
-format- command just changes what is displayed, not what is stored.

For a recent posting in this vein, see
http://www.stata.com/statalist/archive/2013-03/msg00319.html

The -format- assigned to a variable therefore has no consequences for
the results of calculations with them.

The difference between %td and %d is just historic, as I understand
it. They have the same effect, but once more the effect is only on
display.

Nick

On Sun, Mar 10, 2013 at 3:22 PM, Gwinyai Masukume
<[email protected]> wrote:

> Many thanks Rebecca for the very helpful and clear step by step explanation.
>
> Removing the following from your example seems to generate the same results?
> local sd = date("`c(current_date)'","DMY")
> set seed `sd'
> Would the above coding mean the seed would change daily?
>
> Would anyone know if there’s a difference between %d and %td?
>
> /*** full example modified ***/
>  version 12
>  clear
>  set obs 5
> * local sd = date("`c(current_date)'","DMY")
> * set seed `sd'
>
> scalar start = mdy(1,1,1964)
>  scalar end = mdy(12,31,1999)
>  gen date_of_birth = int(start+(end-start)*runiform())
>
> gen today_date=date("`c(current_date)'","DMY")
>
> gen age = round((today_date - date_of_birth)/365.25,0.1)
>  gen age2 = int((today_date - date_of_birth)/365.25)
>
> format date_of_birth today_date %td
>  list, noobs
>  /*** end ***/
>
> Thanks again.
>
> Regards
> G
>
> On 3/10/13, Rebecca Pope <[email protected]> wrote:
>> A correction first.
>>
>> gen month_dob=int(1+(13-1)*runiform())
>> gen day_dob=int(1+(32-1)*runiform())
>> gen year_dob=int(1964+(1999-1964)*runiform())
>> gen date_of_birth=mdy(month_dob, day_dob, year_dob)
>>
>> has the potential to produce an error, albeit with a small
>> probability. Since you are drawing months and days separately, you can
>> draw nonsensical combinations like 2/30. A better method is to draw
>> randomly over the range of dates that you want.
>>
>> scalar start = mdy(1,1,1964)
>> scalar end = mdy(12,31,1999)
>> gen date_of_birth = int(start+(end-start)*runiform())
>> * the two -scalar- assignments can be skipped & placed directly in
>> date_of_birth, but I find this clearer (i.e. just style)
>>
>> You also say:
>>> I’m encountering some problems subtracting these two
>>> dates. Could this be due to date formats i.e. month day year (MDY)
>>> versus day month year (DMY). Or it could be due to one date being an
>>> integer and the other a string? Or it's something else?
>>
>> To refer to the _content_ of c(current_date), you must treat it as a
>> macro: `c(current_date)'. Then use it in the -date()- function to
>> convert to a date value, because yes, you must have two numeric values
>> in order to subtract.
>>
>> gen today_date=date("`c(current_date)'","DMY")
>>
>> Finally:
>>> Theoretically subtracting the today_date from date_of_birth should
>>> give the age,
>>
>> Not so. You should subtract date_of_birth from today_date. This gives
>> you the difference in days from which you can calculate age in
>> (fractional) years. If you don't want to change age until the person
>> has had a birthday, then use the -int()- function.
>>
>> gen age = round((today_date - date_of_birth)/365.25,0.1)
>> gen age2 = int((today_date - date_of_birth)/365.25)
>>
>> /*** full example ***/
>> version 12
>> clear
>> set obs 5
>> local sd = date("`c(current_date)'","DMY")
>> set seed `sd'
>>
>> scalar start = mdy(1,1,1964)
>> scalar end = mdy(12,31,1999)
>> gen date_of_birth = int(start+(end-start)*runiform())
>>
>> gen today_date=date("`c(current_date)'","DMY")
>>
>> gen age = round((today_date - date_of_birth)/365.25,0.1)
>> gen age2 = int((today_date - date_of_birth)/365.25)
>>
>> format date_of_birth today_date %td
>> list, noobs
>> /*** end ***/
>>
>> Output:
>> date_of~h today_d~e age age2
>> -------------------------------------
>> 08sep1983 10mar2013 29.5 29
>> 15jul1965 10mar2013 47.7 47
>> 11dec1970 10mar2013 42.2 42
>> 09jan1978 10mar2013 35.2 35
>> 15oct1987 10mar2013 25.4 25
>>

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index