DATA Step, Macro, Functions and more

Age Calculation

Reply
Regular Contributor
Posts: 217

Age Calculation

[ Edited ]

I've seen some discussions of age calculation (especially when 2/28, 2/29 and 3/1 are involved)...here's my solution:

 

%macro age (bday, eday);

      INTCK('YR', &bday, &eday)-((put(&eday,mmddyyb4.)<(put(&bday,mmddyyb4.))))

%mend age;

 

It can then be called:

 

proc sql;select *,%age(BIRTH_DT, CLAIM_RECD_DT) as age from xdata;

 

-or-

 

data xxx; set xdata; age=%age(BIRTH_DT, CLAIM_RECD_DT); run;

PROC Star
Posts: 1,846

Re: Age Calculation

Posted in reply to tomrvincent

I think the best bet for age and the most robust is using 

 

YRDIF Function

 

\http://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/viewer.htm#p1pmmr2dtec...

 

handles leap years too

Regular Contributor
Posts: 217

Re: Age Calculation

Posted in reply to novinosrin

I wish it did, but it doesn't.

 

birthday of 02/29/1960, date of 02/27/1965: yrdif gives 4.997260274, which is wrong. Should be 4.

 

birthday of 02/29/1960, date of 02/28/1965: yrdif gives 5, which is wrong.  Should also be 4.

 

That's why I came up with the macro.

PROC Star
Posts: 1,846

Re: Age Calculation

Posted in reply to tomrvincent

Ok, looks like you are looking for actuals with an int

 

data w;
age=int(yrdif('29feb1960'd, '27feb1965'd, 'ACT/ACT'));
age1=int(('27feb1965'd-'29feb1960'd)/365.25);
age2=int(yrdif('29feb1960'd, '28feb1965'd, 'ACT/ACT'));
age3=int(('28feb1965'd-'29feb1960'd)/365.25);
put _all_;
run;

 

 

Regular Contributor
Posts: 217

Re: Age Calculation

Posted in reply to novinosrin
That works as well! I will present that to management as an alternative. Thanks!
Regular Contributor
Posts: 217

Re: Age Calculation

Posted in reply to novinosrin
but age4=int(yrdif('29Mar1960'd, '29Mar1965'd, 'ACT/ACT')); gives 4 instead of 5.
Super User
Posts: 23,778

Re: Age Calculation

Posted in reply to tomrvincent

Any reason to not create a function instead of a macro here?


@tomrvincent wrote:

I've seen some discussions of age calculation (especially when 2/28, 2/29 and 3/1 are involved)...here's my solution:

 

%macro age (bday, eday);

      INTCK('YR', &bday, &eday)-((put(&eday,mmddyyb4.)<(put(&bday,mmddyyb4.))))

%mend age;

 

It can then be called:

 

proc sql;select *,%age(BIRTH_DT, CLAIM_RECD_DT) as age from xdata;

 

-or-

 

data xxx; set xdata; age=%age(BIRTH_DT, CLAIM_RECD_DT); run;


 

Regular Contributor
Posts: 217

Re: Age Calculation

You can't use a function in proc sql.
Super User
Posts: 23,778

Re: Age Calculation

Posted in reply to tomrvincent
age=intck('Year', '29feb1960'd, '27feb1965'd, 'C');

 

https://blogs.sas.com/content/sasdummy/2011/07/12/computing-age-in-sas-9-3/

 


@tomrvincent wrote:

I've seen some discussions of age calculation (especially when 2/28, 2/29 and 3/1 are involved)...here's my solution:

 

%macro age (bday, eday);

      INTCK('YR', &bday, &eday)-((put(&eday,mmddyyb4.)<(put(&bday,mmddyyb4.))))

%mend age;

 

It can then be called:

 

proc sql;select *,%age(BIRTH_DT, CLAIM_RECD_DT) as age from xdata;

 

-or-

 

data xxx; set xdata; age=%age(BIRTH_DT, CLAIM_RECD_DT); run;


 

Regular Contributor
Posts: 217

Re: Age Calculation

yeah..I've looked at that...it doesn't work either.  Birthday of 29FEB1952 and event day of 28FEB2017 should be 64, not 65.

 

I didn't just arbitrarily come up with that macro...I compared various combinations of yrdif, intck and so on but they don't correctly handle the 29/28 case.

Trusted Advisor
Posts: 2,125

Re: Age Calculation

Posted in reply to tomrvincent
Ask a Question
Discussion stats
  • 10 replies
  • 85 views
  • 0 likes
  • 4 in conversation