# Calculate age when portion of date of birth is not known

Dear Team,

I know of the functions FullYearsBetween, CenturyMonthCode, and new DateTime. Is there a way to estimate a person’s age when a portion of their date of birth is not known?

Thanks

If you don’t know date of a month when the person was born, just assume he or she was born in the middle of the month (or any time other date you prefer).
Create a `DateTime` variable like this: `new DateTime(year, month, 15)`, where `year` and `month` are variables that you already have and know and use FullYearsBetween to calculate age.

Is it possible to create a conditional statement like this to do it all in one go? 99 is used in data entry when the year, month or day is not known.

dob_year!=99 ?
\$age :
dob_month!=99 ?
\$age_unknown_mt :
dob_day!=99 ?
\$age_unknown_day :
9999

The macros are:
\$age…

`````` 	FullYearsBetween(
new DateTime(dob_year, dob_month, dob_day),
interview_start
)
``````

\$age_unknown_mt…

``````FullYearsBetween(
new DateTime(dob_year, 6, dob_day),
interview_start
)
``````

\$age_unknown_day…

``````FullYearsBetween(
new DateTime(dob_year, dob_month, 15),
interview_start
)``````

If the year is known then your first macro will be effective (regardless whether month and day are responded to). This will lead to an incorrect result, say, for a 2000-99-99 birth date.

So it is probably possible to do everything in one expression, but the expression will need to be different.

Hi Sergiy,

I was getting some issues with the expression, that’s why I posted to the forum. I’ll try to tackle it some more.

Thanks.

Below is code snippet that I’ve used in the past for assigning computed age to a variable when a survey captures date of interview, date of birth, and respondent-reported age. It tries to make the best age calculation depending on which parts of day, month, and year of birth are reported. You may need to adapt (or correct) the code. But the code at least has the values of giving a framework for approaching the problem. The conditional statement is done through C#'s ternary operator.

``````/*
CALCULATION OF AGE WITH THREE CASES OF FIGURE

1. All birth date elements reported
2. Day is the only missing item
3. Months and or day is missing
INPUT
Birth date:
- Day: s02q04a
- Month: s02q04b
- Year: s02q04c
Date of the interview: \$currentDate
Do not know code: \$doNotKnow
OUTPUT
1. Provided information is available, calculated age
2. In the absence of information, a null value
*/

// if all elements of the date of birth are reported
s02q04a.InRange(1,31) && s02q04b.InRange(1,12) &&
s02q04c.InRange(1900, \$currentDate.Value.Year) && \$currentDate.Value.Year != null ?

FullYearsBetween(
new DateTime ( (int) s02q04c, (int) s02q04b, (int) s02q04a ),
\$currentDate
)

:

// if day is missing, assume it is the 15th
s02q04a == \$doNotKnow && s02q04b.InRange(1,12) &&
s02q04c.InRange(1900, \$currentDate.Value.Year)
&& \$currentDate.Value.Year!=null ?

FullYearsBetween(
new DateTime ( (int) s02q04c, (int) s02q04b, (int) 15 ) ,
\$currentDate
)

:

// if month is missing, compute difference between years
(s02q04a.InRange(1,31) || s02q04a==\$doNotKnow) && s02q04b==\$doNotKnow &&
s02q04c.InRange(1900, \$currentDate.Value.Year) && \$currentDate.Value.Year!=null ?

\$currentDate.Value.Year - s02q04c

:

// if year of birth is unknown, take the respondent-reported age
s02q04c == \$doNotKnow ?

s02q05a :

// otherwise, assign a null value
null

``````
1 Like

Hi Arthur,

I will definitely look at your expression. It’s a lot more elegant than what I had in mind.