The syntax at the end of this post was created to compare different ways
that have been suggested in determining age from dates.
Method 2 (my preferred method) uses a DO IF section.
Hope this helps.
Art
[log in to unmask]
Social Research Consultants
University Park, MD USA
(301) 864-5570
new file.
* this program demonstrates and compares different ways of finding the
age as of a given date
* which is different for each case. It can be adapted to use the same
given date for all cases.
* .
*Method 1; note that cases 2,6,11,12,15,and 16 do not have the desired
result.
*Method 2: matches the wanted result for all 16 cases.
*Method 3:note that cases 1, 2, and 6 have the same Gregorian month and
day for birth and admission
* but case 6 has a different Julian date due to leap year.
*Method 4: note the difference between "want" and "age4" for case16.
*.
*make up some data.
data list list / birthdat(adate10) admsndat(adate10) wanted (f3).
begin data.
4/10/1931 4/10/2002 71
4/10/1933 4/10/2002 69
4/10/1932 4/7/2002 69
4/10/1932 4/8/2002 69
4/10/1932 4/9/2002 69
4/10/1932 4/10/2002 70
4/10/1932 4/11/2002 70
4/10/1932 4/12/2002 70
4/10/1932 4/13/2002 70
1/1/2000 1/1/2003 3
1/1/2001 1/1/2003 2
1/1/2002 1/1/2003 1
12/31/2002 1/1/2003 0
1/2/1933 1/1/2003 69
1/1/1933 1/1/2003 70
1/1/1899 1/1/2000 101
end data.
numeric age1 age2 age3 (f3).
*method 1.
COMPUTE age1 = trunc(CTIME.DAYS(admsndat - birthdat) / 365.25) .
*.
* method2 see if birthday in current year has occurred yet.
compute bmo = xdate.month(birthdat).
compute bdom = xdate.mday(birthdat).
compute byr = xdate.year(birthdat).
compute amo = xdate.month(admsndat).
compute adom = xdate.mday(admsndat).
compute ayr = xdate.year(admsndat).
compute nearbirt = date.mdy(bmo,bdom,ayr).
do if nearbirt le admsndat.
compute age2 = ayr-byr.
else if nearbirt gt admsndat.
compute age2 = ayr-byr-1.
end if.
formats nearbirt (adate10) age2 (f3).
*.
* method 3 jdate way from earlier responder.
compute age3 = xdate.year(admsndat) - xdate.year(birthdat)
- ( xdate.jdate(birthdat) > xdate.jdate(admsndat) ).
compute age3 = xdate.year(admsndat) - xdate.year(birthdat)
- ( xdate.jdate(birthdat) gt xdate.jdate(admsndat) ).
compute jadm = xdate.jdate(admsndat) .
compute jborn = xdate.jdate(birthdat) .
formats age3 jborn jadm(f3).
*.
*method 4 =method 1 (modified).
COMPUTE age4 = trunc((1+CTIME.DAYS(admsndat - birthdat)) / 365.25) .
formats age4 (f3).
numeric line (f2).
compute case = $casenum.
list /variables = case birthdat admsndat wanted age1 age2 age3 age4
jborn jadm.
Hamilton, Lorna wrote:
> Can anyone show me a working sample of the DO IF syntax command.
>
> Many thanks.
>
> Lorna Hamilton
> Senior Statistician
>
> BRE Environment
> BRE, Garston, Watford, WD25 9XX
> TEL: +44 (0) 1923 664166
> FAX: +44 (0) 1923 664166
> Web: http://www.bre.co.uk <http://www.bre.co.uk/>
>
> Privileged and confidential information and/or copyright material may be contained in this e-mail. The information and material is intended for the use of the intended addressee only. If you are not the intended addressee you may not copy or deliver it to anyone else or use it in any unauthorised manner. To do so is prohibited and may be unlawful. If you receive this e-mail by mistake, please advise the sender immediately by return e-mail and destroy all copies. Thank you.
>
> Foundation for the Built Environment, Registered under number 3282856 in England and Wales.
> Building Research Establishment Ltd, Registered under number 3319324 in England and Wales.
> BRE Certification Limited, Registered under number 3548352 in England and Wales.
> Registered Offices: Bucknalls Lane, Garston, Watford, Hertfordshire WD25 9XX
>
|