OK, saw it.
Run this:
data have;
input CLIENT_ID BIRTH_DATE :mmddyy10. CHVaccDate_1 :mmddyy10.;
today=MDY(12,01,2016);
AGEtest= floor ((intck('month',BIRTH_DATE,today) - (day(today) < day(BIRTH_DATE))) / 12);*age at study start;
agetest1 = intck('year',BIRTH_DATE,today,'c');
VAGE = INTCK('year',BIRTH_DATE,CHVaccDate_1,"Continuous");
format BIRTH_DATE CHVaccDate_1 mmddyy10.;
datalines;
1 08212000 04062009
2 02032002 04082011
3 05101999 11222010
4 10191999 03052012
;
run;
proc freq;
table agetest agetest1 VAGE;
run;
At least for the set of test data, both calculations for agetest resolve to the same value, and vage also looks correct.
INTCK is tricky, and not a good idea for calculating AGE. It counts the number of boundaries crossed, not the number of intervals. For example:
age = intck('year', '28dec2016'd, '02jan2017'd); ==> 1, because 1 instance of January 1st is encountered
age = intck('year', '02jan2017'd, '31dec2017'd); ==> 0, because 0 instances of January 1st are encountered
You might find it easier to just count days. For example, an approximate formula would be:
if (CHVaccDate_1 - BIRTH_DATE) < (365 * 7) + (366 * 2) then delete;
Or you can experiment with a different function:
if CHVaccDate_1 < intnx('year', BIRTH_DATE, 9, 'same') then delete;
If haven't really tested that last one sufficiently, but there is definitely a combination that would work using INTNX.
INTCK("YEAR", d1, d2, "CONTINUOUS") uses d1 as the year boundary, so it does calculate age (in years) appropriately.
Excellent! Time for me to play with my new toy.
@PGStats solution works like a charm. One sample test program:
data test;
do d='14feb2016'd to '01mar2016'd;
age = intck('year', '20feb2015'd, d, 'continuous');
output;
end;
run;
proc print data=test;
title 'Start = February 20, 2015';
format d date9.;
run;
If you're on at least 9.3, I'd used the yrdif function. e.g.:
age=YRDIF(start-date,end-date);
There is a third parameter you can add, but age is the default for that parameter.
Art, CEO, AnalystFinder.com
My only problem with INTCK for calculating age is how it deals with the leap year situation, i.e., when one is born on 2/29 of a leap year and you are testing it with 2/28 of a leap year. I agree with YRDIF's definition that it should be one year greater than the INTCK calculation.
e.g.:
data have; input CLIENT_ID BIRTH_DATE :mmddyy10. CHVaccDate_1 :mmddyy10.; intck_AGE = INTCK('year',BIRTH_DATE,CHVaccDate_1,"Continuous"); yrdif_age=int(YRDIF(BIRTH_DATE,CHVaccDate_1)); format BIRTH_DATE CHVaccDate_1 mmddyy10.; datalines; 1 08212000 04062009 2 02032002 04082011 3 05101999 11222010 4 10191999 03052012 5 02292012 02282016 6 02292012 02282017 7 02292012 02292016 ; run;
Art, CEO, AnalystFinder.com
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.