Dear List,
I need to create 24 variables that would correspond to 24 months and would indicate the number of events within each month. I have 10 different variables (m_found1 through m_found10) coded 1 if the event took place and 0 and the event did not take place. I also have 9 variables (time_r1 through time_r9) that represent the number of days between the first event and each consequtive event. For example, time_r1 is the number of days between the fisrt and second event, time_r2 is the number of days between the fisrt and third event, etc. I need to count the number of events within the first month after the first event took place, second month, etc. I also have a date for each event (e.g.,Date1, date2, date3, etc.) Basically, I need a sum of m_found1 through m_found10 but I need this sum for each month within 24 month period. Any help will be greatly appreciated, Lana |
At 02:59 PM 9/29/2006, Yampolskaya, Svetlana wrote:
>I need to create 24 variables that would correspond to 24 months and >would indicate the number of events within each month. > >I have 10 different variables (m_found1 through m_found10) coded 1 if >the event took place and 0 and the event did not take place. I need to >count the number of events within the first month after the first >event took place, second month, etc. I have a date for each event >(e.g.,Date1, date2, date3, etc.) > >I need a sum of m_found1 through m_found10 but I need this sum for >each month within 24 month period. I'm assuming you mean calendar months, and that date1, date2, etc., are SPSS date variables. I've dropped mention of your time_r1, time_r2, etc.; I think the best solution uses the 'dateN' variables. A 'long' solution, where each even is in a separate record, is easier, and one way of doing this is 'long to wide to long' - writing a separate record for each event, converting these into one record for each of the 24 months, and recombining into the 24 variables you want. Here's a 'wide' solution, which doesn't do that. It assumes, without checking, that the dates are in ascending order. It isn't tested. NUMERIC INMNTH01 TO INMNTH24 (F3). VAR LABELS INMNTH01 'No. of events in month of 1st event' INMNTH02 'No. of events in month 1 after 1st event' <etc., if desired>. * Make the list of counts by month a vector; and . * start all counts at 0. . VECTOR COUNTS = INMNTH01 TO INMNTH24. RECODE INMNTH01 TO INMNTH24 (ELSE=0). * Start with month1, month when first event occurred. COMPUTE #MONTH# = 1. COMPUTE #MONTH = DATE.MOYR(XDATE.MONTH(date1),XDATE.YEAR(date1)). * The following DO REPEAT would be easier to write, . * and could be made a LOOP if desired, if variables . * mfound1, mfound2, ... mfound10 . * and date1, date2, ... date10 are contiguous in . * the file. . DO REPEAT /* Loop through the list of events */ OCCURS = mfound_1 mfound_2 mfound_3 mfound_4 mfound_5 mfound_6 mfound_7 mfound_8 mfound_9 mfound_10 /EVTDATE = date1 date1 date1 date1 date1 date1 date1 date1 date1 date10. * Month in which event being examined occurred . . COMPUTE #EVTMNTH = = DATE.MOYR(XDATE.MONTH(EVTDATE),XDATE.YEAR(EVTDATE)). . COMPUTE #STRTIDX = #MONTH#. . LOOP #MONTH# = #STRTIDX TO 24. . DO IF #EVTMNTH = #MONTH /* Event 'this' month */. . COMPUTE COUNTS(#MONTH#) = COUNTS(#MONTH#) + OCCURS. . ELSE. /* Event later month */. * Roundabout but reliable way to increment a month by . * one month: . . COMPUTE #IN_NEXT = #MONTH + TIME.DAYS(32). . COMPUTE #MONTH= = DATE.MOYR(XDATE.MONTH(#IN_NEXT), XDATE.YEAR (#IN_NEXT)). . END IF. . END LOOP IF #MONTH = #EVTMNTH. END REPEAT. |
Free forum by Nabble | Edit this page |