Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How many weeks after the beginning of my program?

    Hello guys,

    I really appreciate if you can help me with this. This is a time sensitive request. I have different programs in different units of a mental hospital. to reduce violent incidents. Each of row in my data-set is the incident. The programs started in different times. I would like to create a variable that represents the week of the program.


    I create a variable where 1 is the incident happend after the program started.
    replace prog1=1 if facility==1 & INCIDENT_DATE >= td(14jul2015) & INCIDENT_HOUSE =="2 room left"

    But I need to know the week in which the incident occur.



    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str7 UOF_NO int UOF_INCIDENT_DATE str61 INCIDENT_AREA str30 INCIDENT_HOUSINGUNIT long INCIDENT_FACILITY_RC float progGMDC
    "025" 20472 "Housing Area" "2 UPPER" 1 1
    "059" 20496 "Housing Area" "2 UPPER" 1 1
    "088" 20518 "Housing Area" "2 UPPER" 1 1
    "080" 20512 "Housing Area" "8 UPPER" 1 2
    "087" 20517 "Housing Area" "8 UPPER" 1 2
    end
    format %td UOF_INCIDENT_DATE
    label values INCIDENT_FACILITY_RC INCIDENT_FACILITY_RC
    label def INCIDENT_FACILITY_RC 1 "GMDC", modify

    So my program one started on July 14 2015 and the first incident append on 19jan2016. How many weeks after? my second program started on 9nov2015.

    Best,
    Marvin

  • #2
    Well, first you need to create a variable that shows the start date for the program. If it's a handful of programs you can do that with -gen- and -replace- statements. If it's a large number, you probably should create a crosswalk file between progGMDC and start_date and -merge- it into the data you show.

    Once you have that, you need to clarify what you mean by "how many weeks." There are (at least) three senses of this and they will sometimes yield different numbers:

    1. A week is just any seven day period, so the number of weeks is just the difference between the dates divided by 7:
    Code:
    gen weeks_after = (UOF_INCIDENT_DATE - start_date)/7
    In this metric, if a program starts this Friday and an incident occurs on the following Monday. the latency is 3/7 of a week.

    2. The first week of a year begins on Jan 1 and runs through Jan 7. The second week of the year begins on Jan 8 and runs through Jan 14, etc. The 52nd week of the year, in a non-leap year, would end on Dec 30, except that in Stata, the extra day of Dec 31 is also included in week 52. The current year, 2016, began on a Friday, so these weeks run from Friday through the following Thursday (except for the last week which extends beyond through Dec 31). So in this metric, if a program begins on Thursday and an incident occurs the very next day on a Friday, they are 1 week apart. On the other hand, from Friday to Saturday is a difference of 1 week. To code this version of weeks apart:
    Code:
    gen weeks_after = wofd(UOF_INCIDENT_DATE) - wofd(start_date)
    3. A week is a seven day period beginning on a Sunday (in the US, in many other countries on Monday), and extends through the following Saturday (resp. Sunday). In this metric, the distance from Thursday to Friday is 0 weeks, but from Thursday to the following Monday is 1 week. This kind of week is somewhat harder to deal with and I won't show the code here unless you post back and say that this is exactly what you need.

    As you can see, "week" is a problematic unit of time to deal with, and is, for good reason, seldom used in scientific studies. The only one that has some coherence is the first definition, but it leads to numbers that are ugly when represented in either binary or decimal. The others really don't make much sense for time-keeping purposes. Why not just track time in days, which have only a single definition? (If the number of days yields unwieldy large numbers, you can reduce them to years, dividing by 365, or 365.25, depending on your discipline and the scale of the overall timeframe. This is still more coherent than weeks and less likely to be misunderstood.)

    Comment


    • #3
      Clyde's bottom line is, I think, precisely mine: If weeks seem natural units, divide the number of days by 7. By all means, report any days over too.

      For more on Stata weeks and other ways of dealing with weeks see

      SJ-12-4 dm0065_1 . . . . . Stata tip 111: More on working with weeks, erratum
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
      Q4/12 SJ 12(4):765 (no commands)
      lists previously omitted key reference

      SJ-12-3 dm0065 . . . . . . . . . . Stata tip 111: More on working with weeks
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
      Q3/12 SJ 12(3):565--569 (no commands)
      discusses how to convert data presented in yearly and weekly
      form to daily dates and how to aggregate such data to months
      or longer intervals

      SJ-10-4 dm0052 . . . . . . . . . . . . . . . . Stata tip 68: Week assumptions
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
      Q4/10 SJ 10(4):682--685 (no commands)
      tip on Stata's solution for weeks and on how to set up
      your own alternatives given different definitions of the
      week

      search week, sj will bring up clickable links to .pdf copies of all.

      Comment


      • #4
        this is great! Thank you some much!

        My project has changed a little bit. now I need to show the numbers of incidents by the calendar. For example FEb 1 2015 (monday) to Feb 7 2015(Sunday) should be 2015 Feb week 1. and so on. Even better if the dates are shown such as FEb1toFEb7.

        I try to do

        gen week = wofd(UOF_INCIDENT_DATE)
        format week %tw

        bur I got the following date value 2015w5

        Is there a way to get 2015_Feb_w1 instead 2015w5? is this possible?

        Thank you very much!

        Comment


        • #5
          anyone?

          Comment


          • #6
            I don't think you can do that. The weekly time variables in Stata that you get from wofd() are referenced to 1 Jan 1960. There are also numerical weeks from 1 through 52 that you can get from the week() function, but they are referenced to Jan 1 of the year that the week begins in (and the 52nd week is 8 or 9 days depending on leap year). There is no Stata date form that references weeks beginning at the start of the month. So there is no display format that gives you week relative to the start of its own month.

            Moreover, again, terminology relying on weeks is inherently confusing and ambiguous. Year 2016 began on a Friday. So is week 1 of January 2016 to be a 2 or 3-day week consisting of Jan 1-2 (possibly incl Jan 3), or if week 1 of January the first full week beginning on the first Sunday (or Monday)? May of 2016 begins on a Sunday. So do we have a 2 or 3 day week containing the (29th), 30th, and 31st of that month? [Those are rhetorical questions] Weeks are just inherently confusing as they don't line up well with anything else and I re-emphasize that it is best to avoid them. In fact, I predict that if you continue down this path, things will only get more and more complicated and you will, in little time, find yourself hopelessly lost.

            In any case, what you have asked for is, I'm pretty sure, not possible with Stata date variables and display formats. You might approximate that by creating a string variable along these lines:

            Code:
            clear
            set obs 10
            gen weekly = 2863 + _n
            format weekly %tw
            
            gen wd1 = dofw(weekly)
            gen wd2 = dofw(weekly+1) - 1
            format wd1 wd2 %tdCCYY_Mon_DD
            
            egen str_week = concat(wd1 wd2), punct(-) format(%tdCCYY_Mon_DD)
            
            list, noobs clean
            but these dates are not referenced to the start of the month.

            Comment


            • #7
              Marvin,

              "bumping" is strongly discouraged on this forum. Statalist is not a paid 24 hour help line. Those of us who respond do so voluntarily; we choose what and whom to respond to and we choose when to respond. Nobody is obligated to respond to anything here. Bumping is just irritating. Patience is a virtue--do acquire it!

              Comment


              • #8
                You are absolutely right. This will never happen again. I am sorry one more time and have a nice rest of the day.

                Marvin

                Comment


                • #9
                  Marvin Aliaga I agree with Clyde as usual. There was advice on bumping and related matters in http://www.statalist.org/forums/foru...-similar-topic You are not expected to know about that thread, except that you do now.

                  Comment


                  • #10
                    After reading more closely your reply about the new week date format. I will encourage using month instead of week for the analysis at my job. Thank you!

                    Comment

                    Working...
                    X