DATEDIFF will give you the exact difference in the requested units with truncation. So that matches the age calculation that applies if you are trying to buy a drink or get a driver's license. However, that is not itself a date: it is a duration and, therefore, would not have a date format.

If you want a datelike form, you can compute the parts using the mod function, e.g.,

compute years = mod(delta, 365).

compute resid1 = delta - years * 365.

compute months = mod(resid1, 31).

etc.

where delta is the datediff value with "days" as the unit.

But you have to decide how long a month is. DATEDIFF takes the calendar into account, but the subsequent calculations use a fixed number of days for years and months.

You might also be interested in the DATESUM function, which adds a fixed number of units - days, months, whatever, to a date. It takes into account the actual unit lengths according to the calendar but still uses a constant for the number of units to add.

------------------------------

Jon Peck

------------------------------

Original Message:

Sent: Tue November 30, 2021 10:16 AM

From: xiaoqin Oezener-Wan

Subject: Chronological age

Hello

you can use DATEdiff function:

compute agechr=datediff(aw2, dob, 'days').

agechr is the days between aw2 and dob.

best regards,

------------------------------

xq

Original Message:

Sent: Thu November 25, 2021 07:53 AM

From: Bethan Whiddon

Subject: Chronological age

I'm trying to make a new variable which will be the age at a time point (clinic date and dob). I have used this syntax to get a numeric age: compute agechr=(aw2 - dob) / (365.25 * time.days(1)).

However, I need the format of the age in years, months and days between the two dates. So I made 3 new variables from each date variable made up of the day, month and year. I then minused these variables from each other to find the time between. I thought I'd cracked it until realising I had minus answers from dates that had months or days smaller for the clinic date e.g. clinic in janurary and dob in july (1-7=-6).

The chronological age variable I need assumes that all months have 30 days in them. So I thought maybe I could add 30 days to the day variable and minus 1 for the month when the day is a minus answer, or, add 12 to the month variable and minus 1 for the year when the month is a minus answer. Does anyone know how I would write the syntax to get this to work?... or any other suggestions are welcome.

------------------------------

Beth

------------------------------