Anyone have any ideas on how to calculate the # of days in each month between the beg_date & End_date. Here is my data:
id Beg_Date End_Date 1 04/05/2013 10/05/2013 3 05/01/2013 12/15/2013 11 10/11/2012 05/09/2013 4 08/16/2013 12/25/2013 5 07/14/2013 08/25/2013 8 08/04/2013 11/28/2013 9 11/12/2012 01/15/2014 10 06/02/2011 02/01/2014 I found some code that almost does what I want but it doesn't take into account if the Beg & End date are not within the same year. Here is what I found: VECTOR month(12F8). COMPUTE #oneday=24*60*60. LOOP mth=1 TO 12. COMPUTE beg1=MAX(datein , DATE.dmy(1,mth,XDATE.YEAR(datein))). COMPUTE end1=MIN(dateout + #oneday, DATE.dmy(1,mth+1,XDATE.YEAR(datein))). COMPUTE month(mth)=max(0, CTIME.DAYS(end1 - beg1)). END LOOP. EXECUTE. Any help to adjust the syntax, so it will work with more than 1 year, would be greatly appreciated. Thanks |
Administrator
|
Why not KISS!
COMPUTE days=CTIME.DAYS(End_Date-Beg_Date). or look up the DATEDIFF function. Not sure what the other coder was thinking!
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?" |
Sorry, I should have been more clear. I want to find out how many days are in each month between the beg and end dates. The syntax I found is almost there but only if the beg and end dates are in the same calendar year.
|
Administrator
|
Seems like you really only need to deal with Feb.
Starters would be another loop for year. Need a longer vector. Why is it important to do this? What are you going to do with the info that goes beyond simply overall difference in days between Beg_Date and End_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?" |
Administrator
|
Ah, What the heck...
DATA LIST LIST /id (F2) Beg_Date (ADATE) End_Date (ADATE) . BEGIN DATA 1 04/05/2013 10/05/2013 3 05/01/2013 12/15/2013 11 10/11/2012 05/09/2013 4 08/16/2013 12/25/2013 5 07/14/2013 08/25/2013 8 08/04/2013 11/28/2013 9 11/12/2012 01/15/2014 10 06/02/2011 02/01/2014 END DATA. DATASET NAME raw. COMPUTE Merge=1. COMPUTE @=1. DATASET DECLARE agg. AGGREGATE OUTFILE agg/ BREAK /min=MIN(Beg_Date) / max=MAX(End_Date). DATASET ACTIVATE agg . LOOP Y=XDATE.YEAR(min) TO XDATE.YEAR(max). LOOP M=1 TO 12. IF ANY(M,1,3,5,7,8,10,12) days=31. IF ANY(M,4,6,9,11) days=30. IF M=2 days=28. IF M=2 AND ANY(y,1992,1996,2000,2004, 2008, 2012, 2016,2020,2024) days=29. XSAVE OUTFILE 'calender' / KEEP Y M days. END LOOP. END LOOP. EXECUTE. GET FILE 'calender'. DATASET NAME calender. DATASET ACTIVATE calender. COMPUTE YM=Y+M/100. ALTER TYPE YM (A8). COMPUTE YM=CONCAT("v",LTRIM(YM)). EXECUTE. DELETE VARIABLES Y M . CASESTOVARS INDEX=YM. COMPUTE merge=1. MATCH FILES / FILE raw / TABLE * / BY merge. COMPUTE @1=1. VARSTOCASES /MAKE dates FROM @ TO @1 /INDEX=Source(dates) /KEEP=id Beg_Date End_Date /NULL=KEEP. SELECT IF INDEX(Source,"@")=0 AND RANGE(DATE.MOYR(NUMBER(CHAR.SUBSTR(Source,7,2),F2.0), NUMBER(CHAR.SUBSTR(Source,2,4),F4.0)), DATE.MOYR(XDATE.MONTH(Beg_Date),XDATE.YEAR (Beg_Date)), DATE.MOYR(XDATE.MONTH(End_Date),XDATE.YEAR (End_Date))) . SORT CASES BY id Source. CASESTOVARS /ID=id /INDEX=Source/GROUPBY=VARIABLE.
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?" |
Wow, thank you so much. I'm fairly new to SPSS (1 1/2 years). This is going to be a great help. I work at a hospital and I was asked to figure out how many days per month a patient is in a specific unit, so they could figure out the % per month as opposed to the standard average length of stay.
It kind of threw me for a loop when I was asked. I had the formula working in Excel but couldn't figure out how to rework it for SPSS. When I found the other code I thought I hit the jackpot, but it wasn't quite there. Your code works great. Now I just need to break it down so I can understand it and I've learned a few more neat things. Thanks again, much appreciated. LD |
In reply to this post by ldonaghey
Look at the datediff transformation function.
Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] phone: 720-342-5621 From: ldonaghey <[hidden email]> To: [hidden email], Date: 02/01/2014 09:13 PM Subject: [SPSSX-L] Determining number of days used per month with a beginning & ending date Sent by: "SPSSX(r) Discussion" <[hidden email]> Anyone have any ideas on how to calculate the # of days in each month between the beg_date & End_date. Here is my data: id Beg_Date End_Date 1 04/05/2013 10/05/2013 3 05/01/2013 12/15/2013 11 10/11/2012 05/09/2013 4 08/16/2013 12/25/2013 5 07/14/2013 08/25/2013 8 08/04/2013 11/28/2013 9 11/12/2012 01/15/2014 10 06/02/2011 02/01/2014 I found some code that almost does what I want but it doesn't take into account if the Beg & End date are not within the same year. Here is what I found: VECTOR month(12F8). COMPUTE #oneday=24*60*60. LOOP mth=1 TO 12. COMPUTE beg1=MAX(datein , DATE.dmy(1,mth,XDATE.YEAR(datein))). COMPUTE end1=MIN(dateout + #oneday, DATE.dmy(1,mth+1,XDATE.YEAR(datein))). COMPUTE month(mth)=max(0, CTIME.DAYS(end1 - beg1)). END LOOP. EXECUTE. Any help to adjust the syntax, so it will work with more than 1 year, would be greatly appreciated. Thanks -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Determining-number-of-days-used-per-month-with-a-beginning-ending-date-tp5724275.html Sent from the SPSSX Discussion mailing list archive at Nabble.com. ===================== 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 David Marso
Found one little hiccup. The beg date month needs to calculate # of days left in that month and the end date month needs to calculate up to the end date.
Then, as a double check if I were to add up all the individual months it should equal the simple date substraction of Beg date - End Date = Length of stay (184 including end date). Also if Beg date=End Date then needs to default to 1 day. Sorry hospital stats are a pain! For example: Beg Date 04/05/2013 v2013.04 should have 26 days V2013.05=31 V2013.06=30 V2013.07=31 V2013.08=31 V2013.09=30 End Date 10/05/2013 v2013.10 should have 5 days =184 |
Administrator
|
Se FIRST and LAST functions on MATCH FILES and XDATE.DAY on COMPUTE.
Apply before the CASESTOVARS. ----
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?" |
Administrator
|
This post was updated on .
Oops, that should be XDATE.MDAY ;=(
Changed variable dates to days in VARSTOCASES. After the SORT. MATCH FILES / FILE * / FIRST=@top / LAST=@Bot / BY ID. IF @Top days = days - XDATE.MDAY(Beg_Date)+1. IF @Bot days = XDATE.MDAY(End_Date). IF @Top EQ @Bot days=1. EXECUTE. DATASET CLOSE agg. DATSET CLOSE raw. DELETE VARIABLES @Top @Bot . CASESTOVARS /ID=id /INDEX=Source/GROUPBY=VARIABLE. <complete code> DATA LIST LIST /id (F2) Beg_Date (ADATE) End_Date (ADATE) . BEGIN DATA 1 04/05/2013 10/05/2013 3 05/01/2013 12/15/2013 11 10/11/2012 05/09/2013 4 08/16/2013 12/25/2013 5 07/14/2013 08/25/2013 8 08/04/2013 11/28/2013 9 11/12/2012 01/15/2014 10 06/02/2011 02/01/2014 END DATA. DATASET NAME raw. COMPUTE Merge=1. COMPUTE @=1. DATASET DECLARE agg. AGGREGATE OUTFILE agg/ BREAK /min=MIN(Beg_Date) / max=MAX(End_Date). DATASET ACTIVATE agg . LOOP Y=XDATE.YEAR(min) TO XDATE.YEAR(max). LOOP M=1 TO 12. IF ANY(M,1,3,5,7,8,10,12) days=31. IF ANY(M,4,6,9,11) days=30. IF M=2 days=28. IF M=2 AND ANY(y,1992,1996,2000,2004, 2008, 2012, 2016,2020,2024) days=29. XSAVE OUTFILE 'calender' / KEEP Y M days. END LOOP. END LOOP. EXECUTE. GET FILE 'calender'. DATASET NAME calender. DATASET ACTIVATE calender. COMPUTE YM=Y+M/100. ALTER TYPE YM (A8). COMPUTE YM=CONCAT("v",LTRIM(YM)). EXECUTE. DELETE VARIABLES Y M . CASESTOVARS INDEX=YM. COMPUTE merge=1. MATCH FILES / FILE raw / TABLE * / BY merge. COMPUTE @1=1. VARSTOCASES /MAKE days FROM @ TO @1 /INDEX=Source(days) /KEEP=id Beg_Date End_Date /NULL=KEEP. SELECT IF INDEX(Source,"@")=0 AND RANGE(DATE.MOYR(NUMBER(CHAR.SUBSTR(Source,7,2),F2.0), NUMBER(CHAR.SUBSTR(Source,2,4),F4.0)), DATE.MOYR(XDATE.MONTH(Beg_Date),XDATE.YEAR (Beg_Date)), DATE.MOYR(XDATE.MONTH(End_Date),XDATE.YEAR (End_Date))) . SORT CASES BY id Source. MATCH FILES / FILE * / FIRST=@top / LAST=@Bot / BY ID. IF @Top days = days - XDATE.MDAY(Beg_Date)+1. IF @Bot days = XDATE.MDAY(End_Date). IF @Top EQ @Bot days=1. EXECUTE. DATASET CLOSE agg. DATSET CLOSE raw. DELETE VARIABLES @Top @Bot . CASESTOVARS /ID=id /INDEX=Source/GROUPBY=VARIABLE.
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?" |
Administrator
|
Hmmm,
IF @Top days EQ days - XDATE.MDAY(Beg_Date)+1. IF @Bot days EQ XDATE.MDAY(End_Date). IF @Top EQ @Bot days=1. Should be: IF @Top days = days - XDATE.MDAY(Beg_Date)+1. IF @Bot days = XDATE.MDAY(End_Date). IF @Top EQ @Bot days=1.
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 ldonaghey
Hi Laurinda
Check out the datediff function - it does exactly what you want. See the Universals section, date functions, in the Syntax reference guide Regards, Adrian Adrian Barnett Project Officer Educational Measurement and Analysis Data and Information Systems Department for Education and Child Development "Children and young people are at the centre of everything we do" -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of ldonaghey Sent: Sunday, 2 February 2014 2:43 PM To: [hidden email] Subject: Determining number of days used per month with a beginning & ending date Anyone have any ideas on how to calculate the # of days in each month between the beg_date & End_date. Here is my data: id Beg_Date End_Date 1 04/05/2013 10/05/2013 3 05/01/2013 12/15/2013 11 10/11/2012 05/09/2013 4 08/16/2013 12/25/2013 5 07/14/2013 08/25/2013 8 08/04/2013 11/28/2013 9 11/12/2012 01/15/2014 10 06/02/2011 02/01/2014 I found some code that almost does what I want but it doesn't take into account if the Beg & End date are not within the same year. Here is what I found: VECTOR month(12F8). COMPUTE #oneday=24*60*60. LOOP mth=1 TO 12. COMPUTE beg1=MAX(datein , DATE.dmy(1,mth,XDATE.YEAR(datein))). COMPUTE end1=MIN(dateout + #oneday, DATE.dmy(1,mth+1,XDATE.YEAR(datein))). COMPUTE month(mth)=max(0, CTIME.DAYS(end1 - beg1)). END LOOP. EXECUTE. Any help to adjust the syntax, so it will work with more than 1 year, would be greatly appreciated. Thanks -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Determining-number-of-days-used-per-month-with-a-beginning-ending-date-tp5724275.html Sent from the SPSSX Discussion mailing list archive at Nabble.com. ===================== 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 ===================== 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
|
Adrian (and Jon),
Read the rest of the thread. A simple DATEDIFF function does NOT do the job requested. Neither does the simple CTIME.DAYS that I initially posted!
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 ldonaghey
Dear list!
I have not been following all the postings on this. Here is a previous clumsy old solution of mine. However it needs the time variables to be numeric and the formats in Scandinavian style (ååmmdd, (not in SPSS)). Does not include both admission and discharge day, only one of them. I think the Swedish variable names are self-explanatory (but if not (AR=YEAR, MA=MONTH,DA=DAY, IN=ADMISSION,UT=DISCHARGE, VARDTID=DAYS HOSPITALIZED). COMPUTE #ARIN=TRUNC(INDATUM/10000). COMPUTE #TEMP1=MOD(TRUNC(INDATUM/1000),10). COMPUTE #TEMP2=MOD(TRUNC(INDATUM/100),10). COMPUTE #MAIN=#TEMP1*10+#TEMP2. COMPUTE #MAIN=((MOD(TRUNC(INDATUM/1000),10))*10)+(MOD(TRUNC(INDATUM/100),10)). COMPUTE #DAIN=((MOD(TRUNC(INDATUM/10),10))*10)+MOD(INDATUM,10). COMPUTE #ARUT=TRUNC(UTDATUM/10000). COMPUTE #MAUT=((MOD(TRUNC(UTDATUM/1000),10))*10)+MOD(TRUNC(UTDATUM/100),10). COMPUTE #DAUT=((MOD(TRUNC(UTDATUM/10),10))*10)+MOD(UTDATUM,10). FOR #ARIN TO #DAUT (F2.0). COMPUTE VARDTID=YRMODA(#ARUT,#MAUT,#DAUT)-YRMODA(#ARIN,#MAIN,#DAIN). FORMAT VARDTID (F3.0). VAR LAB VARDTID 'No of days between admission and discharge'. FORMAT VARDTID (F3.0). EXECUTE. best Staffan Lindberg Sweden -----Ursprungligt meddelande----- Från: SPSSX(r) Discussion [mailto:[hidden email]] För David Marso Skickat: den 3 februari 2014 08:22 Till: [hidden email] Ämne: Re: Determining number of days used per month with a beginning & ending date Adrian (and Jon), Read the rest of the thread. A simple DATEDIFF function does NOT do the job requested. Neither does the simple CTIME.DAYS that I initially posted! Barnett, Adrian (DECD) wrote > Hi Laurinda > Check out the datediff function - it does exactly what you want. See > the Universals section, date functions, in the Syntax reference guide > > Regards, > > Adrian > > > Adrian Barnett > Project Officer > Educational Measurement and Analysis > Data and Information Systems > Department for Education and Child Development > > "Children and young people are at the centre of everything we do" > > -----Original Message----- > From: SPSSX(r) Discussion [mailto: > SPSSX-L@.UGA > ] On Behalf Of ldonaghey > Sent: Sunday, 2 February 2014 2:43 PM > To: > SPSSX-L@.UGA > Subject: Determining number of days used per month with a beginning & > ending date > > Anyone have any ideas on how to calculate the # of days in each month > between > the beg_date & End_date. Here is my data: > id Beg_Date End_Date > 1 04/05/2013 10/05/2013 > 3 05/01/2013 12/15/2013 > 11 10/11/2012 05/09/2013 > 4 08/16/2013 12/25/2013 > 5 07/14/2013 08/25/2013 > 8 08/04/2013 11/28/2013 > 9 11/12/2012 01/15/2014 > 10 06/02/2011 02/01/2014 > I found some code that almost does what I want but it doesn't take into > account if the Beg & End date are not within the same year. Here is what > I > found: > > VECTOR month(12F8). > COMPUTE #oneday=24*60*60. > LOOP mth=1 TO 12. > COMPUTE beg1=MAX(datein , DATE.dmy(1,mth,XDATE.YEAR(datein))). > COMPUTE end1=MIN(dateout + #oneday, DATE.dmy(1,mth+1,XDATE.YEAR(datein))). > COMPUTE month(mth)=max(0, CTIME.DAYS(end1 - beg1)). > END LOOP. > EXECUTE. > > Any help to adjust the syntax, so it will work with more than 1 year, > would > be greatly appreciated. Thanks > > > > -- > View this message in context: > http://spssx-discussion.1045642.n5.nabble.com/Determining-number-of-da > ys-used-per-month-with-a-beginning-ending-date-tp5724275.html > Sent from the SPSSX Discussion mailing list archive at Nabble.com. > > ===================== > To manage your subscription to SPSSX-L, send a message to > LISTSERV@.UGA > (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 > > ===================== > To manage your subscription to SPSSX-L, send a message to > LISTSERV@.UGA > (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 ----- 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?" -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Determining-number-of-days-use d-per-month-with-a-beginning-ending-date-tp5724275p5724288.html Sent from the SPSSX Discussion mailing list archive at Nabble.com. ===================== 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 ===================== 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 David Marso
Hi David
The question is how to calculate the number of days between two dates, even when they are in different years. DATEDIFF does not care how far apart the two dates are, so it will work. I can't see what is being overlooked in the question Regards, Adrian Adrian Barnett Project Officer Educational Measurement and Analysis Data and Information Systems Department for Education and Child Development "Children and young people are at the centre of everything we do" -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of David Marso Sent: Monday, 3 February 2014 5:52 PM To: [hidden email] Subject: Re: Determining number of days used per month with a beginning & ending date Adrian (and Jon), Read the rest of the thread. A simple DATEDIFF function does NOT do the job requested. Neither does the simple CTIME.DAYS that I initially posted! Barnett, Adrian (DECD) wrote > Hi Laurinda > Check out the datediff function - it does exactly what you want. See > the Universals section, date functions, in the Syntax reference guide > > Regards, > > Adrian > > > Adrian Barnett > Project Officer > Educational Measurement and Analysis > Data and Information Systems > Department for Education and Child Development > > "Children and young people are at the centre of everything we do" > > -----Original Message----- > From: SPSSX(r) Discussion [mailto: > SPSSX-L@.UGA > ] On Behalf Of ldonaghey > Sent: Sunday, 2 February 2014 2:43 PM > To: > SPSSX-L@.UGA > Subject: Determining number of days used per month with a beginning & > ending date > > Anyone have any ideas on how to calculate the # of days in each month > between > the beg_date & End_date. Here is my data: > id Beg_Date End_Date > 1 04/05/2013 10/05/2013 > 3 05/01/2013 12/15/2013 > 11 10/11/2012 05/09/2013 > 4 08/16/2013 12/25/2013 > 5 07/14/2013 08/25/2013 > 8 08/04/2013 11/28/2013 > 9 11/12/2012 01/15/2014 > 10 06/02/2011 02/01/2014 > I found some code that almost does what I want but it doesn't take into > account if the Beg & End date are not within the same year. Here is what > I > found: > > VECTOR month(12F8). > COMPUTE #oneday=24*60*60. > LOOP mth=1 TO 12. > COMPUTE beg1=MAX(datein , DATE.dmy(1,mth,XDATE.YEAR(datein))). > COMPUTE end1=MIN(dateout + #oneday, DATE.dmy(1,mth+1,XDATE.YEAR(datein))). > COMPUTE month(mth)=max(0, CTIME.DAYS(end1 - beg1)). > END LOOP. > EXECUTE. > > Any help to adjust the syntax, so it will work with more than 1 year, > would > be greatly appreciated. Thanks > > > > -- > View this message in context: > http://spssx-discussion.1045642.n5.nabble.com/Determining-number-of-da > ys-used-per-month-with-a-beginning-ending-date-tp5724275.html > Sent from the SPSSX Discussion mailing list archive at Nabble.com. > > ===================== > To manage your subscription to SPSSX-L, send a message to > LISTSERV@.UGA > (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 > > ===================== > To manage your subscription to SPSSX-L, send a message to > LISTSERV@.UGA > (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 ----- 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?" -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Determining-number-of-days-used-per-month-with-a-beginning-ending-date-tp5724275p5724288.html Sent from the SPSSX Discussion mailing list archive at Nabble.com. ===================== 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 ===================== 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
|
OP needed to determine the number of days within EACH month, Not the entire LOS.
I initially posted an answer using CTIME.DAYS. Read a few messages into the thread.
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 ldonaghey
At 11:12 PM 2/1/2014, ldonaghey wrote:
>Anyone have any ideas on how to calculate the # of days in each >month between the beg_date & End_date. The general idea of looping through calendar months, calculating the number of days in each, is the right one; but I think some of the code has made that harder than it is. You can loop by calendar months without putting any calendar information in the code, by taking advantage of, . Given a date, you can find the calendar month (and, equivalently, the first day of the calendar month) by extracting year and month, and computing DATE.DMY(1,month, year). . Given a calendar month (that is, given its first day), the date 32 days later always falls in the next calendar month. You can then use the above logic to calculate the first day in the next calendar month. . The last day of the current calendar month is one day prior to the first day of the next calendar month. . Given a calendar month and a time interval, the portion of the interval falling in that month begins with the beginning of the interval, but not earlier than the first day of the month; and ends with the end of the interval, but not later than the last day of the month. Like this (with the data from the original posting): |-----------------------------|---------------------------| |Output Created |03-FEB-2014 16:45:57 | |-----------------------------|---------------------------| [TestData] id Beg_Date End_Date 1 04/05/2013 10/05/2013 3 05/01/2013 12/15/2013 11 10/11/2012 05/09/2013 4 08/16/2013 12/25/2013 5 07/14/2013 08/25/2013 8 08/04/2013 11/28/2013 9 11/12/2012 01/15/2014 10 06/02/2011 02/01/2014 Number of cases read: 8 Number of cases listed: 8 C DATASET COPY WorkDSet. DATASET ACTIVATE WorkDSet WINDOW=FRONT. NUMERIC CalMonth (MOYR8) NUMERIC Days (F3). VAR LABEL CalMonth 'Calendar month' Days 'Days of stay during calendar month'. NUMERIC #Yr# #Mo# (F4). NUMERIC #NextMon (MOYR8). NUMERIC #LastDay (ADATE10) /* Last day of current month */. COMPUTE #Yr# = XDATE.YEAR (Beg_Date). COMPUTE #Mo# = XDATE.MONTH(Beg_Date). COMPUTE CalMonth = DATE.DMY(1,#Mo#,#Yr#). STRING @Sp (A2) /* Just a space for the output listing */. COMPUTE @Sp= ' '. LOOP. * Get *a* day in the next month, then the *first* day ... . COMPUTE #NextMon = CalMonth + TIME.DAYS(32). . COMPUTE #Yr# = XDATE.YEAR (#NextMon). . COMPUTE #Mo# = XDATE.MONTH(#NextMon). . COMPUTE #NextMon = DATE.DMY(1,#Mo#,#Yr#). * The last day in THIS month is the day before the ... * first day in the NEXT month ... . COMPUTE #LastDay = #NextMon - TIME.DAYS(1). . COMPUTE Days = CTIME.DAYS(MIN(End_Date,#LastDay) - MAX(Beg_Date,CalMonth)) + 1. . XSAVE OUTFILE = Unroll /KEEP=id @Sp CalMonth Days Beg_Date End_Date. . COMPUTE CalMonth = #NextMon. END LOOP IF #NextMon GT End_Date. EXECUTE. GET FILE = Unroll. LIST. List |-----------------------------|---------------------------| |Output Created |03-FEB-2014 16:45:58 | |-----------------------------|---------------------------| C:\Documents and Settings\Richard\My Documents \Temporary\SPSS\ 2014-02-01 ldonaghey-Determining number of days used per month - ByMonth.SAV [Blank lines inserted manually] id @Sp CalMonth Days Beg_Date End_Date 1 APR 2013 26 04/05/2013 10/05/2013 1 MAY 2013 31 04/05/2013 10/05/2013 1 JUN 2013 30 04/05/2013 10/05/2013 1 JUL 2013 31 04/05/2013 10/05/2013 1 AUG 2013 31 04/05/2013 10/05/2013 1 SEP 2013 30 04/05/2013 10/05/2013 1 OCT 2013 5 04/05/2013 10/05/2013 3 MAY 2013 31 05/01/2013 12/15/2013 3 JUN 2013 30 05/01/2013 12/15/2013 3 JUL 2013 31 05/01/2013 12/15/2013 3 AUG 2013 31 05/01/2013 12/15/2013 3 SEP 2013 30 05/01/2013 12/15/2013 3 OCT 2013 31 05/01/2013 12/15/2013 3 NOV 2013 30 05/01/2013 12/15/2013 3 DEC 2013 15 05/01/2013 12/15/2013 11 OCT 2012 21 10/11/2012 05/09/2013 11 NOV 2012 30 10/11/2012 05/09/2013 11 DEC 2012 31 10/11/2012 05/09/2013 11 JAN 2013 31 10/11/2012 05/09/2013 11 FEB 2013 28 10/11/2012 05/09/2013 11 MAR 2013 31 10/11/2012 05/09/2013 11 APR 2013 30 10/11/2012 05/09/2013 11 MAY 2013 9 10/11/2012 05/09/2013 4 AUG 2013 16 08/16/2013 12/25/2013 4 SEP 2013 30 08/16/2013 12/25/2013 4 OCT 2013 31 08/16/2013 12/25/2013 4 NOV 2013 30 08/16/2013 12/25/2013 4 DEC 2013 25 08/16/2013 12/25/2013 5 JUL 2013 18 07/14/2013 08/25/2013 5 AUG 2013 25 07/14/2013 08/25/2013 8 AUG 2013 28 08/04/2013 11/28/2013 8 SEP 2013 30 08/04/2013 11/28/2013 8 OCT 2013 31 08/04/2013 11/28/2013 8 NOV 2013 28 08/04/2013 11/28/2013 9 NOV 2012 19 11/12/2012 01/15/2014 9 DEC 2012 31 11/12/2012 01/15/2014 9 JAN 2013 31 11/12/2012 01/15/2014 9 FEB 2013 28 11/12/2012 01/15/2014 9 MAR 2013 31 11/12/2012 01/15/2014 9 APR 2013 30 11/12/2012 01/15/2014 9 MAY 2013 31 11/12/2012 01/15/2014 9 JUN 2013 30 11/12/2012 01/15/2014 9 JUL 2013 31 11/12/2012 01/15/2014 9 AUG 2013 31 11/12/2012 01/15/2014 9 SEP 2013 30 11/12/2012 01/15/2014 9 OCT 2013 31 11/12/2012 01/15/2014 9 NOV 2013 30 11/12/2012 01/15/2014 9 DEC 2013 31 11/12/2012 01/15/2014 9 JAN 2014 15 11/12/2012 01/15/2014 10 JUN 2011 29 06/02/2011 02/01/2014 10 JUL 2011 31 06/02/2011 02/01/2014 10 AUG 2011 31 06/02/2011 02/01/2014 10 SEP 2011 30 06/02/2011 02/01/2014 10 OCT 2011 31 06/02/2011 02/01/2014 10 NOV 2011 30 06/02/2011 02/01/2014 10 DEC 2011 31 06/02/2011 02/01/2014 10 JAN 2012 31 06/02/2011 02/01/2014 10 FEB 2012 29 06/02/2011 02/01/2014 10 MAR 2012 31 06/02/2011 02/01/2014 10 APR 2012 30 06/02/2011 02/01/2014 10 MAY 2012 31 06/02/2011 02/01/2014 10 JUN 2012 30 06/02/2011 02/01/2014 10 JUL 2012 31 06/02/2011 02/01/2014 10 AUG 2012 31 06/02/2011 02/01/2014 10 SEP 2012 30 06/02/2011 02/01/2014 10 OCT 2012 31 06/02/2011 02/01/2014 10 NOV 2012 30 06/02/2011 02/01/2014 10 DEC 2012 31 06/02/2011 02/01/2014 10 JAN 2013 31 06/02/2011 02/01/2014 10 FEB 2013 28 06/02/2011 02/01/2014 10 MAR 2013 31 06/02/2011 02/01/2014 10 APR 2013 30 06/02/2011 02/01/2014 10 MAY 2013 31 06/02/2011 02/01/2014 10 JUN 2013 30 06/02/2011 02/01/2014 10 JUL 2013 31 06/02/2011 02/01/2014 10 AUG 2013 31 06/02/2011 02/01/2014 10 SEP 2013 30 06/02/2011 02/01/2014 10 OCT 2013 31 06/02/2011 02/01/2014 10 NOV 2013 30 06/02/2011 02/01/2014 10 DEC 2013 31 06/02/2011 02/01/2014 10 JAN 2014 31 06/02/2011 02/01/2014 10 FEB 2014 1 06/02/2011 02/01/2014 Number of cases read: 82 Number of cases listed: 82 AGGREGATE OUTFILE=* /BREAK = CalMonth /TotDays 'Total days of stay, over all patients' = SUM(Days). . FORMATS TotDays (F5). LIST. List |-----------------------------|---------------------------| |Output Created |03-FEB-2014 16:45:58 | |-----------------------------|---------------------------| CalMonth TotDays JUN 2011 29 JUL 2011 31 AUG 2011 31 SEP 2011 30 OCT 2011 31 NOV 2011 30 DEC 2011 31 JAN 2012 31 FEB 2012 29 MAR 2012 31 APR 2012 30 MAY 2012 31 JUN 2012 30 JUL 2012 31 AUG 2012 31 SEP 2012 30 OCT 2012 52 NOV 2012 79 DEC 2012 93 JAN 2013 93 FEB 2013 84 MAR 2013 93 APR 2013 116 MAY 2013 133 JUN 2013 120 JUL 2013 142 AUG 2013 193 SEP 2013 180 OCT 2013 160 NOV 2013 148 DEC 2013 102 JAN 2014 46 FEB 2014 1 Number of cases read: 33 Number of cases listed: 33 ================================= APPENDIX: Test data, and all code ================================= * C:\Documents and Settings\Richard\My Documents . * \Technical\spssx-l\Z-2014\ . * 2014-02-01 ldonaghey-Determining number of days used per month.SPS . * In response to posting . * Date: Sat, 1 Feb 2014 20:12:44 -0800 . * From: ldonaghey <[hidden email]> . * Subject: Determining number of days used per month with a . * beginning & ending date . * To: [hidden email] . * To determine the number of days falling into each calendar month . * Scratch file, in which to unroll to one record per id per . * calendar month FILE HANDLE Unroll /NAME='C:\Documents and Settings\Richard\My Documents' + '\Temporary\SPSS\' + '2014-02-01 ldonaghey-' + 'Determining number of days used per month' + ' - ' + 'ByMonth.SAV'. * Test data, from original post: ................. . NEW FILE. DATA LIST LIST /id Beg_Date End_Date (F3, ADATE10, ADATE10). BEGIN DATA 1 04/05/2013 10/05/2013 3 05/01/2013 12/15/2013 11 10/11/2012 05/09/2013 4 08/16/2013 12/25/2013 5 07/14/2013 08/25/2013 8 08/04/2013 11/28/2013 9 11/12/2012 01/15/2014 10 06/02/2011 02/01/2014 END DATA. DATASET NAME TestData WINDOW=FRONT. LIST. DATASET COPY WorkDSet. DATASET ACTIVATE WorkDSet WINDOW=FRONT. NUMERIC CalMonth (MOYR8) NUMERIC Days (F3). VAR LABEL CalMonth 'Calendar month' Days 'Days of stay during calendar month'. NUMERIC #Yr# #Mo# (F4). NUMERIC #NextMon (MOYR8). NUMERIC #LastDay (ADATE10) /* Last day of current month */. COMPUTE #Yr# = XDATE.YEAR (Beg_Date). COMPUTE #Mo# = XDATE.MONTH(Beg_Date). COMPUTE CalMonth = DATE.DMY(1,#Mo#,#Yr#). STRING @Sp (A2) /* Just a space for the output listing */. COMPUTE @Sp= ' '. LOOP. * Get *a* day in the next month, then the *first* day ... . COMPUTE #NextMon = CalMonth + TIME.DAYS(32). . COMPUTE #Yr# = XDATE.YEAR (#NextMon). . COMPUTE #Mo# = XDATE.MONTH(#NextMon). . COMPUTE #NextMon = DATE.DMY(1,#Mo#,#Yr#). * The last day in THIS month is the day before the ... * first day in the NEXT month ... . COMPUTE #LastDay = #NextMon - TIME.DAYS(1). . COMPUTE Days = CTIME.DAYS(MIN(End_Date,#LastDay) - MAX(Beg_Date,CalMonth)) + 1. . XSAVE OUTFILE = Unroll /KEEP=id @Sp CalMonth Days Beg_Date End_Date. . COMPUTE CalMonth = #NextMon. END LOOP IF #NextMon GT End_Date. EXECUTE. GET FILE = Unroll. LIST. AGGREGATE OUTFILE=* /BREAK = CalMonth /TotDays 'Total days of stay, over all patients' = SUM(Days). . FORMATS TotDays (F5). 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 |
At 04:59 PM 2/3/2014, I wrote:
>. Given a date, you can find the calendar month (and, equivalently, >the first day of the calendar month) by extracting year and month, >and computing >DATE.DMY(1,month, year). That works correctly; but an equivalent, and preferred, form would be DATE.MOYR(month,year). ===================== 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 ldonaghey
At 10:23 AM 2/2/2014, ldonaghey wrote:
>Then, as a double check if I were to add up all the individual >months it should equal the simple date substraction of Beg date - >End Date = Length of stay (184 including end date). Actually, length of stay is Beg date - End date + 1 day. Think of it: if you're hospitalized from Monday through Friday, the difference of the dates is 4 days, but your length of stay is 5. ===================== 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 |
I'm not sure that Monday evening to Friday morning
counts as 5 days. It is 4 nights at a hotel. What are your billing rules? - That's what mattered to the people (and they were just figuring that out), the one time that I consulted on a hospital system. People who disappeared without checking out were also problematic... especially if they came back (mental hospital) a day or two later. Did someone imply that hospital data is a bitch, or something like that? If you are in and out one day, someone did point out that the difference of 0 means 1. Probably. If you are in and out of three units in one day, that difference of 0 equals 3 days. Possibly, depending on your purpose. In that case, the sum-of-stays equals to the length of time plus the number of admissions. Hospital data is a bitch. -- Rich Ulrich ---------------------------------------- > Date: Tue, 4 Feb 2014 00:18:53 -0500 > From: [hidden email] > Subject: Re: Determining number of days used per month with a beginning & ending date > To: [hidden email] > > At 10:23 AM 2/2/2014, ldonaghey wrote: > >>Then, as a double check if I were to add up all the individual >>months it should equal the simple date substraction of Beg date - >>End Date = Length of stay (184 including end date). > > Actually, length of stay is Beg date - End date + 1 day. Think of it: > if you're hospitalized from Monday through Friday, the difference of > the dates is 4 days, but your length of stay is 5. > > ===================== > 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 ===================== 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 |
Free forum by Nabble | Edit this page |