Announcement

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

  • Create flag loops based on week

    Hello, I have a question that somewhat builds off my question a couple days ago. I have daily data, with one variable being when someone attends a program.

    We are hoping to create a dummy variable with value 1 (otherwise 0) for the six days following if they attended on a Thursday (which is the day they are supposed to attend typically).

    So, if someone is a "1" on Thursday, they should be a 1 on the Friday, Saturday, Sunday, Monday, Tuesday, and Wednesday following, and then it resets on the next Thursday.

    It's also possible, though rare, for someone to have attended on a day later than Thursday--so if someone's first "1" value is on a Monday, then we'd want their Tuesday and Wednesday to also be a 1 (and then it would still reset on Thursday). Basically, whenever their first "1" is within a Thursday-to-Thursday week, we want to make sure they have 1's for each day following until it resets the following Thursday.

    I hope this description makes sense, I know it is a bit of a roundabout issue. I have been trying various bysort and egen combinations with limited success so far. Thank you in advance for the assistance.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id date attend week_day)
    16324 21923 . 4
    16324 21924 0 5
    16324 21925 1 6
    16324 21926 0 0
    16324 21927 1 1
    16324 21928 1 2
    16324 21929 1 3
    16324 21930 1 4
    16324 21931 1 5
    16324 21932 1 6
    16324 21933 1 0
    16324 21934 1 1
    16324 21935 1 2
    16324 21936 1 3
    16324 21937 1 4
    16324 21938 1 5
    16324 21939 1 6
    16324 21940 1 0
    16324 21941 1 1
    16324 21942 1 2
    16324 21943 1 3
    16324 21944 1 4
    16324 21945 0 5
    16324 21946 1 6
    16324 21947 1 0
    16324 21948 1 1
    16324 21949 1 2
    16324 21950 1 3
    16324 21951 1 4
    16324 21952 1 5
    16324 21953 1 6
    16324 21954 1 0
    16324 21955 1 1
    16324 21956 1 2
    16324 21957 1 3
    16324 21958 0 4
    16324 21959 0 5
    16324 21960 0 6
    16324 21961 0 0
    16324 21962 0 1
    16324 21963 0 2
    16324 21964 0 3
    16324 21965 1 4
    16324 21966 1 5
    16324 21967 0 6
    16324 21968 0 0
    16324 21969 1 1
    16324 21970 1 2
    16324 21971 1 3
    16324 21972 1 4
    16324 21973 1 5
    16324 21974 1 6
    16324 21975 1 0
    16324 21976 1 1
    16324 21977 1 2
    16324 21978 1 3
    16324 21979 1 4
    16324 21980 1 5
    16324 21981 1 6
    16324 21982 1 0
    16324 21983 1 1
    16324 21984 0 2
    16324 21985 1 3
    16324 21986 1 4
    16324 21987 . 5
    16324 21988 . 6
    16324 21989 . 0
    16324 21990 . 1
    16324 21991 . 2
    16324 21992 . 3
    16324 21993 . 4
    16324 21994 . 5
    16324 21995 . 6
    16324 21996 . 0
    16324 21997 . 1
    16324 21998 . 2
    16324 21999 . 3
    16324 22000 . 4
    16324 22001 . 5
    16324 22002 . 6
    16324 22003 . 0
    16324 22004 . 1
    16324 22005 . 2
    16324 22006 . 3
    16324 22007 0 4
    16324 22008 0 5
    16324 22009 0 6
    16324 22010 0 0
    16324 22011 0 1
    16324 22012 0 2
    16324 22013 0 3
    16324 22014 0 4
    16324 22015 0 5
    16324 22016 0 6
    16324 22017 0 0
    16324 22018 0 1
    16324 22019 0 2
    16324 22020 0 3
    16324 22021 . 4
    16324 22022 . 5
    end
    format %tdnn/dd/CCYY date


  • #2
    This is a start.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id date attend week_day)
    16324 21923 . 4
    16324 21924 0 5
    16324 21925 1 6
    16324 21926 0 0
    16324 21927 1 1
    16324 21928 1 2
    16324 21929 1 3
    16324 21930 1 4
    16324 21931 1 5
    16324 21932 1 6
    16324 21933 1 0
    16324 21934 1 1
    16324 21935 1 2
    16324 21936 1 3
    16324 21937 1 4
    16324 21938 1 5
    16324 21939 1 6
    16324 21940 1 0
    16324 21941 1 1
    16324 21942 1 2
    16324 21943 1 3
    16324 21944 1 4
    16324 21945 0 5
    16324 21946 1 6
    16324 21947 1 0
    16324 21948 1 1
    16324 21949 1 2
    16324 21950 1 3
    16324 21951 1 4
    16324 21952 1 5
    16324 21953 1 6
    16324 21954 1 0
    16324 21955 1 1
    16324 21956 1 2
    16324 21957 1 3
    16324 21958 0 4
    16324 21959 0 5
    16324 21960 0 6
    16324 21961 0 0
    16324 21962 0 1
    16324 21963 0 2
    16324 21964 0 3
    16324 21965 1 4
    16324 21966 1 5
    16324 21967 0 6
    16324 21968 0 0
    16324 21969 1 1
    16324 21970 1 2
    16324 21971 1 3
    16324 21972 1 4
    16324 21973 1 5
    16324 21974 1 6
    16324 21975 1 0
    16324 21976 1 1
    16324 21977 1 2
    16324 21978 1 3
    16324 21979 1 4
    16324 21980 1 5
    16324 21981 1 6
    16324 21982 1 0
    16324 21983 1 1
    16324 21984 0 2
    16324 21985 1 3
    16324 21986 1 4
    16324 21987 . 5
    16324 21988 . 6
    16324 21989 . 0
    16324 21990 . 1
    16324 21991 . 2
    16324 21992 . 3
    16324 21993 . 4
    16324 21994 . 5
    16324 21995 . 6
    16324 21996 . 0
    16324 21997 . 1
    16324 21998 . 2
    16324 21999 . 3
    16324 22000 . 4
    16324 22001 . 5
    16324 22002 . 6
    16324 22003 . 0
    16324 22004 . 1
    16324 22005 . 2
    16324 22006 . 3
    16324 22007 0 4
    16324 22008 0 5
    16324 22009 0 6
    16324 22010 0 0
    16324 22011 0 1
    16324 22012 0 2
    16324 22013 0 3
    16324 22014 0 4
    16324 22015 0 5
    16324 22016 0 6
    16324 22017 0 0
    16324 22018 0 1
    16324 22019 0 2
    16324 22020 0 3
    16324 22021 . 4
    16324 22022 . 5
    end
    format %tdnn/dd/CCYY date
    
    gen thursday = date if attend == 1 & week_day == 4 
    bysort id (date) : replace thursday = thursday[_n-1] if missing(thursday)
    gen wanted = attend 
    by id: replace wanted = wanted[_n-1] if missing(attend) & inrange(date-thursday, 1, 6) 
    
    list, sepby(wanted)
    
         +-----------------------------------------------------------+
         |    id        date   attend   week_day   thursday   wanted |
         |-----------------------------------------------------------|
      1. | 16324    1/9/2020        .          4          .        . |
         |-----------------------------------------------------------|
      2. | 16324   1/10/2020        0          5          .        0 |
         |-----------------------------------------------------------|
      3. | 16324   1/11/2020        1          6          .        1 |
         |-----------------------------------------------------------|
      4. | 16324   1/12/2020        0          0          .        0 |
         |-----------------------------------------------------------|
      5. | 16324   1/13/2020        1          1          .        1 |
      6. | 16324   1/14/2020        1          2          .        1 |
      7. | 16324   1/15/2020        1          3          .        1 |
      8. | 16324   1/16/2020        1          4      21930        1 |
      9. | 16324   1/17/2020        1          5      21930        1 |
     10. | 16324   1/18/2020        1          6      21930        1 |
     11. | 16324   1/19/2020        1          0      21930        1 |
     12. | 16324   1/20/2020        1          1      21930        1 |
     13. | 16324   1/21/2020        1          2      21930        1 |
     14. | 16324   1/22/2020        1          3      21930        1 |
     15. | 16324   1/23/2020        1          4      21937        1 |
     16. | 16324   1/24/2020        1          5      21937        1 |
     17. | 16324   1/25/2020        1          6      21937        1 |
     18. | 16324   1/26/2020        1          0      21937        1 |
     19. | 16324   1/27/2020        1          1      21937        1 |
     20. | 16324   1/28/2020        1          2      21937        1 |
     21. | 16324   1/29/2020        1          3      21937        1 |
     22. | 16324   1/30/2020        1          4      21944        1 |
         |-----------------------------------------------------------|
     23. | 16324   1/31/2020        0          5      21944        0 |
         |-----------------------------------------------------------|
     24. | 16324    2/1/2020        1          6      21944        1 |
     25. | 16324    2/2/2020        1          0      21944        1 |
     26. | 16324    2/3/2020        1          1      21944        1 |
     27. | 16324    2/4/2020        1          2      21944        1 |
     28. | 16324    2/5/2020        1          3      21944        1 |
     29. | 16324    2/6/2020        1          4      21951        1 |
     30. | 16324    2/7/2020        1          5      21951        1 |
     31. | 16324    2/8/2020        1          6      21951        1 |
     32. | 16324    2/9/2020        1          0      21951        1 |
     33. | 16324   2/10/2020        1          1      21951        1 |
     34. | 16324   2/11/2020        1          2      21951        1 |
     35. | 16324   2/12/2020        1          3      21951        1 |
         |-----------------------------------------------------------|
     36. | 16324   2/13/2020        0          4      21951        0 |
     37. | 16324   2/14/2020        0          5      21951        0 |
     38. | 16324   2/15/2020        0          6      21951        0 |
     39. | 16324   2/16/2020        0          0      21951        0 |
     40. | 16324   2/17/2020        0          1      21951        0 |
     41. | 16324   2/18/2020        0          2      21951        0 |
     42. | 16324   2/19/2020        0          3      21951        0 |
         |-----------------------------------------------------------|
     43. | 16324   2/20/2020        1          4      21965        1 |
     44. | 16324   2/21/2020        1          5      21965        1 |
         |-----------------------------------------------------------|
     45. | 16324   2/22/2020        0          6      21965        0 |
     46. | 16324   2/23/2020        0          0      21965        0 |
         |-----------------------------------------------------------|
     47. | 16324   2/24/2020        1          1      21965        1 |
     48. | 16324   2/25/2020        1          2      21965        1 |
     49. | 16324   2/26/2020        1          3      21965        1 |
     50. | 16324   2/27/2020        1          4      21972        1 |
     51. | 16324   2/28/2020        1          5      21972        1 |
     52. | 16324   2/29/2020        1          6      21972        1 |
     53. | 16324    3/1/2020        1          0      21972        1 |
     54. | 16324    3/2/2020        1          1      21972        1 |
     55. | 16324    3/3/2020        1          2      21972        1 |
     56. | 16324    3/4/2020        1          3      21972        1 |
     57. | 16324    3/5/2020        1          4      21979        1 |
     58. | 16324    3/6/2020        1          5      21979        1 |
     59. | 16324    3/7/2020        1          6      21979        1 |
     60. | 16324    3/8/2020        1          0      21979        1 |
     61. | 16324    3/9/2020        1          1      21979        1 |
         |-----------------------------------------------------------|
     62. | 16324   3/10/2020        0          2      21979        0 |
         |-----------------------------------------------------------|
     63. | 16324   3/11/2020        1          3      21979        1 |
     64. | 16324   3/12/2020        1          4      21986        1 |
     65. | 16324   3/13/2020        .          5      21986        1 |
     66. | 16324   3/14/2020        .          6      21986        1 |
     67. | 16324   3/15/2020        .          0      21986        1 |
     68. | 16324   3/16/2020        .          1      21986        1 |
     69. | 16324   3/17/2020        .          2      21986        1 |
     70. | 16324   3/18/2020        .          3      21986        1 |
         |-----------------------------------------------------------|
     71. | 16324   3/19/2020        .          4      21986        . |
     72. | 16324   3/20/2020        .          5      21986        . |
     73. | 16324   3/21/2020        .          6      21986        . |
     74. | 16324   3/22/2020        .          0      21986        . |
     75. | 16324   3/23/2020        .          1      21986        . |
     76. | 16324   3/24/2020        .          2      21986        . |
     77. | 16324   3/25/2020        .          3      21986        . |
     78. | 16324   3/26/2020        .          4      21986        . |
     79. | 16324   3/27/2020        .          5      21986        . |
     80. | 16324   3/28/2020        .          6      21986        . |
     81. | 16324   3/29/2020        .          0      21986        . |
     82. | 16324   3/30/2020        .          1      21986        . |
     83. | 16324   3/31/2020        .          2      21986        . |
     84. | 16324    4/1/2020        .          3      21986        . |
         |-----------------------------------------------------------|
     85. | 16324    4/2/2020        0          4      21986        0 |
     86. | 16324    4/3/2020        0          5      21986        0 |
     87. | 16324    4/4/2020        0          6      21986        0 |
     88. | 16324    4/5/2020        0          0      21986        0 |
     89. | 16324    4/6/2020        0          1      21986        0 |
     90. | 16324    4/7/2020        0          2      21986        0 |
     91. | 16324    4/8/2020        0          3      21986        0 |
     92. | 16324    4/9/2020        0          4      21986        0 |
     93. | 16324   4/10/2020        0          5      21986        0 |
     94. | 16324   4/11/2020        0          6      21986        0 |
     95. | 16324   4/12/2020        0          0      21986        0 |
     96. | 16324   4/13/2020        0          1      21986        0 |
     97. | 16324   4/14/2020        0          2      21986        0 |
     98. | 16324   4/15/2020        0          3      21986        0 |
         |-----------------------------------------------------------|
     99. | 16324   4/16/2020        .          4      21986        . |
    100. | 16324   4/17/2020        .          5      21986        . |
         +-----------------------------------------------------------+
    Here is a sketch of more general code (completely untested)

    Code:
    sort id date 
    gen work = .  
    gen wanted = attend 
    local D = 6 
    
    foreach d in 4 5 6 0 1 2  { 
          replace work = cond(attend == 1 & week_day == `d', date, .)   
          by id : replace work = work[_n-1] if missing(work)
          by id: replace wanted = wanted[_n-1] if missing(wanted) & inrange(date - work, 1, `D') 
          local D = `D' - 1 
    } 
    
    list, sepby(wanted)

    Comment


    • #3
      Thanks Nick Cox . The -inrange- function was the big missing element in my prior attempts.

      But it seems like, in your first code, the issue still persists--for example, on 1/12/20, the "wanted" var should have a 1, since it's a Sunday and the day prior has a 1. Ditto for 1/30/20 as another example. I think it may be because of the
      Code:
      by id: replace
      line?
      Last edited by Anne Todd; 11 Dec 2020, 12:37.

      Comment


      • #4
        Sorry for the second reply, but I think I better understand now what your code did: it looks like it just replaced missing values with 1s anytime there was a 1 on a Thursday, with one example being on 3/13/20.

        I'm actually only interested in changing the 0s to 1s, I plan to leave the missings as they are. Sorry I did not make that clear originally.

        Comment

        Working...
        X