SPSS Statistics

 View Only
  • 1.  Chronological age

    Posted Mon November 29, 2021 11:21 PM
    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
    ------------------------------


  • 2.  RE: Chronological age

    Posted Tue November 30, 2021 12:58 AM
    Hi, Bethan. Consider the DATEDIFF function. Here is an excerpt from the Command Syntax Reference manual:

    Date differences

    The DATEDIFF function calculates the difference between two date values and returns an integer (with any fraction component truncated) in the specified date/time units. The general form of the expression is DATEDIFF(datetime2, datetime1, "unit") where datetime2 and datetime1 are both date or time format variables (or numeric values that represent valid date/time values), and "unit" is one of the following string literal values, enclosed in quotes:
    • Years
    • Quarters
    • Months
    • Weeks
    • Days
    • Hours
    • Minutes
    • Seconds
    Example 1

    DATA LIST FREE /date1 date2 (2ADATE10).

    BEGIN DATA
    1/1/2004 2/1/2005 1/1/2004 2/15/2005 1/30/2004 1/29/2005
    END DATA.
    COMPUTE years=DATEDIFF(date2, date1, "years").

    • The result will be the integer portion of the number of years between the two dates, with any fractional component truncated.
    • One "year" is defined as the same month and day, one year before or after the second date argument.
    • For the first two cases, the result is 1, since in both cases the number of years is greater than or equal to 1 and less than 2.
    • For the third case, the result is 0, since the difference is one day short of a year based on the definition of year.
    Example 2

    DATA LIST FREE /date1 date2 (2ADATE10).
    BEGIN DATA
    1/1/2004 2/1/2004 1/1/2004 2/15/2004 1/30/2004 2/1/2004
    END DATA.
    COMPUTE months=DATEDIFF(date2, date1, "months").

    • The result will be the integer portion of the number of months between the two dates, with any fractional component truncated.
    • One "month" is defined as the same day of the month, one calendar month before or after the second date argument.


    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM
    ------------------------------



  • 3.  RE: Chronological age

    Posted Tue November 30, 2021 10:16 AM
    Hello

    you can use DATEdiff function: 

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

    agechr is the days between aw2 and dob.

    best regards,

    ------------------------------
    xq
    ------------------------------



  • 4.  RE: Chronological age

    Posted Tue November 30, 2021 12:11 PM
    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
    ------------------------------



  • 5.  RE: Chronological age

    Posted Tue November 30, 2021 12:18 PM
    p.s. Full details on how DATESUM works are in the CSR.  If you need that function, read that explanation carefully as there are some tricky issues.

    --