Calulating age

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

Calulating age

How can age be  calculated from the date of birth field?
Date of BirthAge
1/1/200012
1/5/200012
1/10/200012
1/1/200111
1/5/200111
1/10/200111
1/1/200210
1/5/200210
1/10/200210

Accepted Solutions
Solution
‎12-23-2012 04:00 PM
PROC Star
Posts: 7,356

Re: Calulating age

If you have 9.3 it is quite easy.  e.g.:

data want;

  informat Date_of_Birth mmddyy10.;

  format Date_of_Birth mmddyy10.;

  input Date_of_Birth;

  age=int(yrdif(Date_of_Birth, today(), 'AGE'));

  cards;

1/1/2000

1/5/2000

1/10/2000

1/1/2001

1/5/2001

1/10/2001

1/1/2002

1/5/2002

1/10/2002

;

If you don't have 9.3 take a look at:

36977 - Using the YRDIF function to calculate age can give incorrect results in SAS® releases pr...

View solution in original post


All Replies
Solution
‎12-23-2012 04:00 PM
PROC Star
Posts: 7,356

Re: Calulating age

If you have 9.3 it is quite easy.  e.g.:

data want;

  informat Date_of_Birth mmddyy10.;

  format Date_of_Birth mmddyy10.;

  input Date_of_Birth;

  age=int(yrdif(Date_of_Birth, today(), 'AGE'));

  cards;

1/1/2000

1/5/2000

1/10/2000

1/1/2001

1/5/2001

1/10/2001

1/1/2002

1/5/2002

1/10/2002

;

If you don't have 9.3 take a look at:

36977 - Using the YRDIF function to calculate age can give incorrect results in SAS® releases pr...

Super User
Super User
Posts: 6,495

Re: Calulating age

You need to use a formula as YRDIF with the "AGE" option still does not calculate AGE the way people do.   It works better in SAS 9.3 but it still seems to have a bug to me.  Try these examples.  YRDIF in SAS 9.3 gets the first one wrong. YRDIF in SAS 9.2 gets both of them wrong.

data age;

  input (dob event ) (:date.) expected ;

  format dob event mmddyy10.;

  age1 = int(yrdif(dob,event,'age'));

  age2 = intck('year',dob,event) - (put(dob,mmddyy4.) > put(event,mmddyy4.)) ;

  if age1 ne expected then  put 'YRDIF wrong ' (_all_) (=);

  if age2 ne expected then  put 'Formula wrong ' (_all_) (=);

cards;

29FEB1960 28FEB1965 4

01JAN1960 31DEC1963 3

run;


YRDIF wrong dob=02/29/1960 event=02/28/1965 expected=4 age1=5 age2=4

YRDIF wrong dob=01/01/1960 event=12/31/1963 expected=3 age1=4 age2=3

Respected Advisor
Posts: 4,640

Re: Calulating age

To calulate age, please read discussion

https://communities.sas.com/thread/39473

Please read the WHOLE discussion.

PG

PG
PROC Star
Posts: 7,356

Re: Calulating age

and and sasthebest:

The yrdif function appears to be correct with 9.3 M1

The common (according to Wikipedia at least) birthday for Feb 29th people is Feb 28th. see: Leap year - Wikipedia, the free encyclopedia

"A person born on February 29 may be called a "leapling" or a "leaper".[19] In common years they usually celebrate their birthdays on February 28. In some situations, March 1 is used as the birthday in a non-leap year since it is the day following February 28."


As for the Jan 1st birthdays, that appears to have been resolved.  When I run Tom's code:


data age;

  input (dob event ) (:date.) expected ;

  format dob event mmddyy10.;

  age1 = int(yrdif(dob,event,'age'));

  age2 = intck('year',dob,event) - (put(dob,mmddyy4.) > put(event,mmddyy4.)) ;

  put (_all_) (=);

cards;

29FEB1960 27FEB1965 4

29FEB1960 28FEB1965 4

29FEB1960 01MAR1965 5

29MAR1960 28JAN1965 4

29MAR1960 29MAR1965 5

29MAR1960 30MAR1965 5

01JAN1960 31DEC1963 3

01JAN1960 01JAN1964 4

01JAN1960 02JAN1964 4

run;

I get:

dob=02/29/1960 event=02/27/1965 expected=4 age1=4 age2=4

dob=02/29/1960 event=02/28/1965 expected=4 age1=5 age2=4

dob=02/29/1960 event=03/01/1965 expected=5 age1=5 age2=5

dob=03/29/1960 event=01/28/1965 expected=4 age1=4 age2=4

dob=03/29/1960 event=03/29/1965 expected=5 age1=5 age2=5

dob=03/29/1960 event=03/30/1965 expected=5 age1=5 age2=5

dob=01/01/1960 event=12/31/1963 expected=3 age1=3 age2=3

dob=01/01/1960 event=01/01/1964 expected=4 age1=4 age2=4

dob=01/01/1960 event=01/02/1964 expected=4 age1=4 age2=4

The only place the calculations differ is with respect to Feb 29th birthdays and the yrdif function looks, to me, to provide the more correct result.

Super Contributor
Posts: 1,636

Re: Calulating age

Hi Art, Tom, PG and the OP,

Happy HolidaysSmiley HappySmiley HappySmiley HappySmiley Happy!!!!

- Linlin

PROC Star
Posts: 7,356

Re: Calulating age

: Same to you, and everyone else, as well.

Super User
Super User
Posts: 6,495

Re: Calulating age

Thanks Art.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 682 views
  • 0 likes
  • 5 in conversation