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   Rebecca Pope <[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   Sun, 10 Mar 2013 11:10:15 -0500

Gwinyai,
It surprises me that you got the same result. That seems to be an
extraordinary coincidence. If I run the code without setting the seed,
I do not get the same results.

date_of~h today_d~e age age2
-------------------------------------
06dec1968 10mar2013 44.3 44
26feb1987 10mar2013 26 26
29jan1984 10mar2013 29.1 29
08oct1985 10mar2013 27.4 27
17aug1988 10mar2013 24.6 24

Note that the dates are different from the original post.

That said, it was a poor choice of seed to send you; the conceit is of
course in thinking that you are close enough to my time-zone to have
not changed dates yet.

Yes, that method of setting the seed will change by date if you leave
it in your program as is. If you are going to keep this code for
replication later, you should determine today's date in Stata dates,
-di date("`c(current_date)'',"DMY")-, and hard code this into your .do
file or else choose some other value. I often use my birthday, ZIP
code, or phone extension since I didn't choose any of them and hence
can be thought of as a pseudo-random place to start.

With respect to %d vs %td, I can't help you I'm afraid. I've always
used %t formats for dates. It is habit for me (I didn't even notice
that you'd omitted the "t" until you pointed it out). I did a quick
search in the manual & didn't see anything helpful. Perhaps someone
else will have an answer.

Regards,
Rebecca

On Sun, Mar 10, 2013 at 10:22 AM, 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
>>
>> Regards,
>> Rebecca
>>
>> *
>> *   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/
>>
>
> *
> *   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/

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