# Calculate age by using birthdate and a specific date?

11 messages
Open this post in threaded view
|
Report Content as Inappropriate

## Calculate age by using birthdate and a specific date?

 Hi everyone, I'm new to learning syntax and am having trouble calculating age in years (AGE) by using birthdate (BDATE) and a specific date (SDATE) which is May 15, 2010. The birthdate format is: YYYYMMDD. The resulting age would preferably be a precise number although an approximation is ok too. I checked the archives and tried to adapt some of the examples which use "Compute" with the Date Extraction function but haven’t been able to make a workable syntax.  Could someone pls. help? Many thanks. lynn
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Calculate age by using birthdate and a specific date?

 Hi Lynn, Check out DATEDIFF: http://publib.boulder.ibm.com/infocenter/spssstat/v20r0m0/topic/com.ibm.spss.statistics.help/syn_date_and_time_date_differences.htm Alex From:        Baz Spami <[hidden email]> To:        [hidden email] Date:        10/11/2011 09:42 AM Subject:        Calculate age by using birthdate and a specific date? Sent by:        "SPSSX(r) Discussion" <[hidden email]> Hi everyone, I'm new to learning syntax and am having trouble calculating age in years (AGE) by using birthdate (BDATE) and a specific date (SDATE) which is May 15, 2010. The birthdate format is: YYYYMMDD. The resulting age would preferably be a precise number although an approximation is ok too. I checked the archives and tried to adapt some of the examples which use "Compute" with the Date Extraction function but haven’t been able to make a workable syntax.  Could someone pls. help? Many thanks. lynn
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Calculate age by using birthdate and a specific date?

 In reply to this post by lynn Hi Lynn.  Look up the DATEDIFF function (under "Universals") in the fine manual.  You'll find examples there.  ;-) lynn wrote Hi everyone, I'm new to learning syntax and am having trouble calculating age in years (AGE) by using birthdate (BDATE) and a specific date (SDATE) which is May 15, 2010. The birthdate format is: YYYYMMDD. The resulting age would preferably be a precise number although an approximation is ok too. I checked the archives and tried to adapt some of the examples which use "Compute" with the Date Extraction function but haven’t been able to make a workable syntax.  Could someone pls. help? Many thanks. lynn -- Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/"When all else fails, RTFM." NOTE: My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Calculate age by using birthdate and a specific date?

 In reply to this post by lynn Lynn Everything you need is on pp 98ff of the manual which you can examine by clicking: Help > Command Syntax Reference I keep a permanent copy on my desktop to consult when I don’t have SPSS open. If you’re new to syntax, there are more than 400 pages of syntax-based tutorials on my website, starting from scratch with raw data and with step-by-step full colour screenshots at each step.  The site also has links to other useful sites with SPSS tutorials.  John F Hall       From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Baz SpamiSent: 11 October 2011 12:32To: [hidden email]Subject: Calculate age by using birthdate and a specific date? Hi everyone,I'm new to learning syntax and am having trouble calculating age in years (AGE) by using birthdate (BDATE) and a specific date (SDATE) which is May 15, 2010. The birthdate format is: YYYYMMDD. The resulting age would preferably be a precise number although an approximation is ok too.I checked the archives and tried to adapt some of the examples which use "Compute" with the Date Extraction function but haven’t been able to make a workable syntax.  Could someone pls. help?Many thanks.lynn
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Calculate age by using birthdate and a specific date?

 In reply to this post by lynn For starters, how about posting your non working syntax and any error messages etc. BTW YYYYMMDD is *NOT* a date format.  You'll need to parse it into a proper date. lynn wrote Hi everyone, I'm new to learning syntax and am having trouble calculating age in years (AGE) by using birthdate (BDATE) and a specific date (SDATE) which is May 15, 2010. The birthdate format is: YYYYMMDD. The resulting age would preferably be a precise number although an approximation is ok too. I checked the archives and tried to adapt some of the examples which use "Compute" with the Date Extraction function but haven’t been able to make a workable syntax.  Could someone pls. help? Many thanks. lynn Please reply to the list and not to my personal email. Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Calculate age by using birthdate and a specific date?

 In reply to this post by lynn Many thanks to Alex Reutter, Bruce Weaver, David Marso, and Rick Oliver for your invaluable help in providing two key pieces of information which resolved my problem in figuring out how to calculate age based on birthdate (YYYYMMDD) and a specified date (May 15, 2010).  It seems I needed to apply a two-step procedure as noted below: 1. David Marso’s observation that YYYYMMDD (BDATE) wasn’t an SPSS “date format” was a key starting point so the data was first converted to an SPSS date renamed “DOB” using the mm/dd/yyyy format. (Thanks to Rick Oliver for suggesting the Time and Date Wizard and also found the syntax code on Raynald's SPSS Tools, Code Archives, Tips, FAQ and Tutorials website.) COMPUTE DOB=date.dmy(number(substr(ltrim(BDATE),7,2),f2.0), number(substr(ltrim(BDATE),5,2),f2.0),   number(substr(ltrim(BDATE),1,4),f4.0)). VARIABLE LABELS DOB ''. VARIABLE LEVEL  DOB (SCALE). FORMATS DOB (ADATE10). VARIABLE WIDTH  DOB(10). EXECUTE. *Note: The specified date (SDATE) was also reformatted to the mm/dd/yyyy format (05/15/2010).  2. Finally, per Alex Reutter and Bruce Weaver, the DATEDIFF function was applied to calculate AGE by subtracting DOB from SDATE.    COMPUTE age = (DATEDIFF(SDATE,DOB,"years")). EXECUTE .   Thanks also to John Hall for your suggestion of accessing the reference manual and your website. Again, MANY thanks to all!  lynn From: Baz Spami <[hidden email]>To: "[hidden email]" <[hidden email]>Sent: Tuesday, October 11, 2011 12:32 AMSubject: Calculate age by using birthdate and a specific date? Hi everyone, I'm new to learning syntax and am having trouble calculating age in years (AGE) by using birthdate (BDATE) and a specific date (SDATE) which is May 15, 2010. The birthdate format is: YYYYMMDD. The resulting age would preferably be a precise number although an approximation is ok too. I checked the archives and tried to adapt some of the examples which use "Compute" with the Date Extraction function but haven’t been able to make a workable syntax.  Could someone pls. help? Many thanks. lynn
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Calculate age by using birthdate and a specific date?

 In reply to this post by lynn At 06:32 AM 10/11/2011, Baz Spami wrote: >I'm having trouble calculating age in years (AGE) by using birthdate >(BDATE) and a specific date (SDATE) which is May 15, 2010. The >birthdate format is: YYYYMMDD. The resulting age would preferably be >a precise number although an approximation is ok too. To use SPSS's date-differencing tools, you need to have both dates in SPSS's own date format. Here's tested code to convert BDATE to SPSS date form, *if it is numeric* (there's different logic if BDATE is a character string): NUMERIC @BYear @BMon @BDay (F4). COMPUTE @BDay  = MOD(BDATE,100). COMPUTE @BYear = (BDATE-@BDay)/100. COMPUTE @BMon  = MOD(@BYear,100). COMPUTE @BYear = (@BYear-@BMon)/100. NUMERIC SPSSBdt (DATE11). COMPUTE SPSSBdt = DATE.MDY(@BMon,@BDay,@BYear). LIST. List |-----------------------------|---------------------------| |Output Created               |11-OCT-2011 16:47:32       | |-----------------------------|---------------------------| [TestDates]     BDATE @BYear @BMon @BDay     SPSSBdt 20111011  2011     10    11 11-OCT-2011 19440609  1944      6     9 09-JUN-1944 17760704  1776      7     4 04-JUL-1776 Number of cases read:  3    Number of cases listed:  3 Then, you need your particular date as an SPSS-form date as well: NUMERIC @SDate  (DATE11). COMPUTE @SDate = DATE.MDY(5,15,2010) /* May 15, 2010 */. Now you have two options: a.) Use DATEDIFF to get the difference between the two dates in whole years, allowing correctly for the oddities of the calendar; b.) Subtract the two values, which gives their difference in seconds, and convert to years and fractions with the appropriate factor. For example, if you take the year as having 365.2425 days, NUMERIC  Elapsed (F7.2). COMPUTE  Elapsed = CTIME.DAYS(@Sdate-SPSSBdt)/365.2425. Finally, if you change the names of the variables that begin with '@' to begin with '#' instead, they'll be scratch variables, and won't appear in your output file. But code with scratch variables can be harder to trace and debug. ============================= APPENDIX: Test data, and code ============================= NEW FILE. DATA LIST LIST /BDATE (F8). BEGIN DATA 20111011 19440609 17760704 END DATA. DATASET NAME TestDates. NUMERIC @BYear @BMon @BDay (F4). COMPUTE @BDay  = MOD(BDATE,100). COMPUTE @BYear = (BDATE-@BDay)/100. COMPUTE @BMon  = MOD(@BYear,100). COMPUTE @BYear = (@BYear-@BMon)/100. NUMERIC SPSSBdt (DATE11). COMPUTE SPSSBdt = DATE.MDY(@BMon,@BDay,@BYear). LIST. ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Calculate age by using birthdate and a specific date?

Open this post in threaded view
|
Report Content as Inappropriate

## Re: Calculate age by using birthdate and a specific date?

Open this post in threaded view
|
Report Content as Inappropriate

## Re: Calculate age by using birthdate and a specific date?

 In reply to this post by lynn Thank you very much for the one-line code, David. Amazing how it compresses everything into a single line.  Thanks again for both the syntax and suggestion about full function names. lynn ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD