Announcement

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

  • Creating a loop to count the weeks before and after a policy

    Hi,

    I'm working on understanding the impact of a hospital policy on healthcare utilization. The policy was implemented in March of 2020. I need to create variable that increases by 1 for every week after the policy and decreases by 1 for every week before the policy. I know I could do it by creating a lot of replace statements e.g.

    gen policy_week=0 if week==11 & year==2020
    replace policy_week=1 if week==12 & year==2020
    replace policy_week=2 if week==13 & year==2020
    replace policy_week=3 if week==14 & year==2020
    replace policy_week=4 if week==15 & year==2020
    .
    .
    .
    replace policy_week=-1 if week==10 & year==2020
    replace policy_week=-2 if week==9 & year==2020
    replace policy_week=-3if week==8 & year==2020
    replace policy_week=-4 if week==7 & year==2020

    But I would like to try it doing a loop. Any guidance on how to best do this?

  • #2
    For your examples, this alone, and no loop, is needed

    Code:
    gen policy_week = week - 11 if year == 2020
    but a deeper problem is how many years you have and how you are holding what appear to be weekly data.

    Stata's weekly dates aren't obviously the solution without more detail on what weeks mean to you, together with years. For example, if there can be a week 53 and/or if weeks can span two years, then you need something else.

    Comment


    • #3
      Thank you for the clarification, Nick. Yes, the time spans two years from March 2019 - March 2021. The policy was implemented on March 15, 2021, but because I don't have data for everyday I was thinking I could roll up the daily dates to week. I was just reading up on your 'Stata tip 68: Week assumptions' to better understand some of the limitations weeks in stata. What is the better solution?
      Last edited by Lindsey Yates; 03 Aug 2022, 08:24.

      Comment


      • #4
        In general by far the best strategy is to use daily dates if you have them, so your counter is just a daily date MINUS mdy(3, 15, 2021)

        Comment


        • #5
          Thank you Nick! I meant that the policy was implemented on 3/15/2020 (not 2021), but the code worked.

          Comment

          Working...
          X