## Calculating a person's age between a start date and end date

Solved
Super Contributor
Posts: 286

# Calculating a person's age between a start date and end date

I want to find all students in my data set who will turn 6 between 07/01/2011 and 09/01/2012.   Suggestions?   here's what the data looks like:

 StudentID StudentName Grade Sex LEP BirthDate Age 10007 Smith, John PK Male 9 05/24/2007 4 730010000865 Jones, Lucy 08 Male 9 06/10/1997 14 730010010121 Miller, Sam 03 Male 9 09/11/2002 8 10236 Smith, Anna 01 Female 9 09/20/2004 6 10538 Garcia, Fred PK Male 1 07/22/2007 4

Accepted Solutions
Solution
‎08-26-2011 02:08 PM
PROC Star
Posts: 8,163

## Calculating a person's age between a start date and end date

Gregg,

I think that you may not have specified your dates correctly but, if you did, I think that the following code would do something quite close to what you want:

%let predate=30JUN2011;

%let enddate=01SEP2012;

data want;

set have;

age_at_predate = floor

((intck('month',birthdate,"&predate"d)

- (day("&predate"d) < day(birthdate))) / 12);

if age_at_predate in (4,5) then do;

age_at_enddate = floor

((intck('month',birthdate,"&enddate"d)

- (day("&enddate"d) < day(birthdate))) / 12);

if age_at_enddate ge 6 then party="yes";

end;

run;

All Replies
Solution
‎08-26-2011 02:08 PM
PROC Star
Posts: 8,163

## Calculating a person's age between a start date and end date

Gregg,

I think that you may not have specified your dates correctly but, if you did, I think that the following code would do something quite close to what you want:

%let predate=30JUN2011;

%let enddate=01SEP2012;

data want;

set have;

age_at_predate = floor

((intck('month',birthdate,"&predate"d)

- (day("&predate"d) < day(birthdate))) / 12);

if age_at_predate in (4,5) then do;

age_at_enddate = floor

((intck('month',birthdate,"&enddate"d)

- (day("&enddate"d) < day(birthdate))) / 12);

if age_at_enddate ge 6 then party="yes";

end;

run;

Super Contributor
Posts: 286

Valued Guide
Posts: 653

## Calculating a person's age between a start date and end date

There are a number of ways of calculating age http://www.wuss.org/proceedings10/coders/2943_4_COD-Carpenter.pdf , however for this problem a simplier solution is available.  Try the INTNX function with the 'SAME' option as the fourth argument to advance the date of birth 6 years.

data students;

input StudentID \$    BirthDate :mmddyy10.;

dob6 = intnx('year',birthdate,6,'s');

if '01jul2011'd le dob6 le '01sep2012'd then note='In Range';

format birthdate dob6 date9.;

datalines;

10007        05/24/2007

73001     06/10/1997

73002     09/11/2002

10236        09/20/2004

10538        07/22/2004

10538        07/22/2005

10538        07/22/2006

10538        07/22/2007

run;

proc print data=students;

run;

🔒 This topic is solved and locked.