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;
I think the best bet for age and the most robust is using
YRDIF Function
handles leap years too
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.
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;
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;
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;
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.
A precise macro was created years ago by Billy Kreuter. See
http://www.pauldickman.com/teaching/sas/age.php
or
http://support.sas.com/publishing/authors/extras/61860_update.pdf
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.