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 |
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 |
Administrator
|
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. ;-)
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
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 Spami 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 |
Administrator
|
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.
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?" |
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 AM 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 |
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 |
Administrator
|
In reply to this post by lynn
Here is a somewhat more elegant technique for calculating the DOB piece of this.
No need for explicit numeric conversion or use of the date aggregation function. Simply convert a proper string date representation (in this case constructed by CONCAT) to a number and apply a format. data list / BDate 1-8 (A). begin data 19581114 end data. compute DOB=NUMBER(CONCAT(SUBSTR(BDate ,1,4),"/",SUBSTR(BDate ,5,2),"/",SUBSTR(BDate ,7,2)),SDATE). FORMATS DOB(Sdate). list. If you are running low on diskspace that can be abbreviated to COMP DOB=NUM(CON(SUB(BDATE,1,4),"/",SUB(BDATE,5,2),"/",SUB(BDATE,7,2)),SDATE). However I urge one and all to use full function names so you know WTF you were doing 6 months from now (or whoever is tasked with maintaining your code if you get hit by a wayward bus). HTH, David
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?" |
In reply to this post by Richard Ristow
Thank you very much, Richard Ristow, for the detailed annotation of the procedure to calculate age based on YYYYMMDD data. This was very helpful and offers a different way of handling the procedure by using Compute to parse the birthdate information into 3 separate variables. I also greatly appreciate your comments in describing each step which informed my understanding of the process and I was able to replicate your example then apply the procedure to my own data. Your procedure worked flawlessly. Thank you again for showing a different way in approaching the problem. lynn ________________________________ From: Richard Ristow <[hidden email]> To: Baz Spami <[hidden email]>; [hidden email] Sent: Tuesday, October 11, 2011 11:19 AM Subject: Re: Calculate age by using birthdate and a specific date? 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. 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. 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. |
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 |
In reply to this post by lynn
Lynn Here’s some syntax generated from the date and time wizard to take (European format) date, month, year and convert them. This was for my restoration project on the 1986 British Social Attitudes survey. Just for fun I worked out what day the interviews were completed, but I need a nice variable like happiness or depression to see if it’s true about “blue Monday”. * Date and Time Wizard: date1 . COMPUTE date1=DATE.DMY(dateintD, dateintM, dateintY). VARIABLE LABELS date1 "Date completed Quaire 1". VARIABLE LEVEL date1 (SCALE). FORMATS date1 (EDATE8). VARIABLE WIDTH date1(8). execute. * Date and Time Wizard: date2 . COMPUTE date2=DATE.DMY(SQDate2D, SQDate2M, SQDate2Y). VARIABLE LABELS date2 "Date completed Quaire 2". VARIABLE LEVEL date2 (SCALE). FORMATS date2 (EDATE8). VARIABLE WIDTH date2 (8). EXECUTE. compute Day = xdate.wkday (date1) . format day (f1.0) . var lab day 'Day of week interviewed'. val lab day 1 'Sunday' 2 'Monday' 3 'Tuesday' 4 'Wednesday' 5 'Thursday' 6 'Friday' 7 'Saturday' . There aren’t any measures of psychological well-being in this survey, but there in my Quality of Life in Britain series. The latter no actual date of birth recorded, but with your data you could possibly test out the old nursery rhyme. Monday's child is fair of face, John F Hall From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Baz Spami 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.
lynn From: Baz Spami <[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 |
Free forum by Nabble | Edit this page |