Announcement

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

  • Filling missing observations based on multiple conditions

    Hi. My dataset contains a state variable, a binary intervention variable (Stepone), and two time variables - month and week number. The week number reflects the week in which the intervention was rolled out. So, the intervention variable could switch rom 0 to 1 in the middle of a month. That is there could be observations within the same month, where the intervention variable could be 0 for some and 1 for others.

    I want to create a categorical variable (early_late) that categorizes states by month of intervention adoption into early (coded as 1) and late (coded as 2) adopters. States that adopted the intervention in months 3-5 will be categorized as early adopters for every time period starting at month the month they adopted. If a state adopted the intervention in month 4, early_late will be 0 for observations corresponding to month 3, but all observations starting month 4 will have early_late==1. States that adopted the intervention in month 6 will be categorized as late adopters for every time period starting at month 6, while all months before that will be coded to 0.

    I've tried several variations of the code below, but haven't managed to figure it out. Any help would be much appreciated.


    Code:
    
    gen early_late=1 if inrange(month, 3, 5) & Stepone==1
    (1,522 missing values generated)
    
    . bysort state weeknum: replace early_late = early_late[_n-1] if early_late >= .
    (0 real changes made)
    
    
    clear
    input str50 state float(Stepone weeknum month early_late)
    
    "Puducherry" 0 12  5 .
    "Puducherry" 0 13  5 .
    "Puducherry" 0 14  6 .
    "Puducherry" 0 15  6 .
    "Puducherry" 0 16  6 .
    "Puducherry" 0 17  6 .
    "Puducherry" 0 18  7 .
    "Puducherry" 0 19  7 .
    "Puducherry" 1 20  7 .
    "Puducherry" 1 21  7 .
    "Puducherry" 1 22  8 .
    "Puducherry" 1 23  8 .
    "Puducherry" 1 24  8 .
    "Puducherry" 1 25  8 .
    "Puducherry" 1 26  8 .
    "Puducherry" 1 27  9 .
    "Puducherry" 1 28  9 .
    "Puducherry" 1 29  9 .
    "Puducherry" 1 30  9 .
    "Puducherry" 1 31 10 .
    "Puducherry" 1 32 10 .
    "Puducherry" 1 33 10 .
    "Puducherry" 1 34 10 .
    "Puducherry" 1 35 10 .
    "Puducherry" 1 36 11 .
    "Puducherry" 1 37 11 .
    "Puducherry" 1 38 11 .
    "Puducherry" 1 39 11 .
    "Puducherry" 1 40 12 .
    "Puducherry" 1 41 12 .
    "Puducherry" 1 42 12 .
    "Puducherry" 1 43 12 .
    "Puducherry" 1 44 13 .
    "Puducherry" 1 45 13 .
    "Puducherry" 1 46 13 .
    "Puducherry" 1 47 13 .
    "Puducherry" 1 48 13 .
    "Punjab"     0  1  3 .
    "Punjab"     0  2  3 .
    "Punjab"     1  3  3 1
    "Punjab"     1  4  3 1
    "Punjab"     1  5  4 1
    "Punjab"     1  6  4 1
    "Punjab"     1  7  4 1
    "Punjab"     1  8  4 1
    "Punjab"     1  9  5 1
    "Punjab"     1 10  5 1
    "Punjab"     1 11  5 1
    "Punjab"     1 12  5 1
    "Punjab"     1 13  5 1
    "Punjab"     1 14  6 .
    "Punjab"     1 15  6 .
    "Punjab"     1 16  6 .
    "Punjab"     1 17  6 .
    "Punjab"     1 18  7 .
    "Punjab"     1 19  7 .
    "Punjab"     1 20  7 .
    "Punjab"     1 21  7 .
    "Punjab"     1 22  8 .
    "Punjab"     1 23  8 .
    "Punjab"     1 24  8 .
    "Punjab"     1 25  8 .
    "Punjab"     1 26  8 .
    "Punjab"     1 27  9 .
    "Punjab"     1 28  9 .
    "Punjab"     1 29  9 .
    "Punjab"     1 30  9 .
    "Punjab"     1 31 10 .
    "Punjab"     1 32 10 .
    "Punjab"     1 33 10 .
    "Punjab"     1 34 10 .
    "Punjab"     1 35 10 .
    "Punjab"     1 36 11 .
    "Punjab"     1 37 11 .
    "Punjab"     1 38 11 .
    "Punjab"     1 39 11 .
    "Punjab"     1 40 12 .
    "Punjab"     1 41 12 .
    "Punjab"     1 42 12 .
    "Punjab"     1 43 12 .
    "Punjab"     1 44 13 .
    "Punjab"     1 45 13 .
    "Punjab"     1 46 13 .
    "Punjab"     1 47 13 .
    "Punjab"     1 48 13 .

  • #2
    Consider:

    Code:
    egen byte adopt_month = min(cond(Stepone == 1, month, .)), by(state)
    gen byte wanted = cond(month < adopt_month , 0, cond(inrange(adopt_month, 3, 5), 1, 2))
    which produces:
    Code:
    . list state Stepone weeknum month wanted, noobs sepby(month)
    
      +-------------------------------------------------+
      |      state   Stepone   weeknum   month   wanted |
      |-------------------------------------------------|
      | Puducherry         0        12       5        0 |
      | Puducherry         0        13       5        0 |
      |-------------------------------------------------|
      | Puducherry         0        14       6        0 |
      | Puducherry         0        15       6        0 |
      | Puducherry         0        16       6        0 |
      | Puducherry         0        17       6        0 |
      |-------------------------------------------------|
      | Puducherry         0        18       7        2 |
      | Puducherry         0        19       7        2 |
      | Puducherry         1        20       7        2 |
      | Puducherry         1        21       7        2 |
      |-------------------------------------------------|
      | Puducherry         1        22       8        2 |
      | Puducherry         1        23       8        2 |
      | Puducherry         1        24       8        2 |
      | Puducherry         1        25       8        2 |
      | Puducherry         1        26       8        2 |
      |-------------------------------------------------|
      | Puducherry         1        27       9        2 |
      | Puducherry         1        28       9        2 |
      | Puducherry         1        29       9        2 |
      | Puducherry         1        30       9        2 |
      |-------------------------------------------------|
      | Puducherry         1        31      10        2 |
      | Puducherry         1        32      10        2 |
      | Puducherry         1        33      10        2 |
      | Puducherry         1        34      10        2 |
      | Puducherry         1        35      10        2 |
      |-------------------------------------------------|
      | Puducherry         1        36      11        2 |
      | Puducherry         1        37      11        2 |
      | Puducherry         1        38      11        2 |
      | Puducherry         1        39      11        2 |
      |-------------------------------------------------|
      | Puducherry         1        40      12        2 |
      | Puducherry         1        41      12        2 |
      | Puducherry         1        42      12        2 |
      | Puducherry         1        43      12        2 |
      |-------------------------------------------------|
      | Puducherry         1        44      13        2 |
      | Puducherry         1        45      13        2 |
      | Puducherry         1        46      13        2 |
      | Puducherry         1        47      13        2 |
      | Puducherry         1        48      13        2 |
      |-------------------------------------------------|
      |     Punjab         0         1       3        1 |
      |     Punjab         0         2       3        1 |
      |     Punjab         1         3       3        1 |
      |     Punjab         1         4       3        1 |
      |-------------------------------------------------|
      |     Punjab         1         5       4        1 |
      |     Punjab         1         6       4        1 |
      |     Punjab         1         7       4        1 |
      |     Punjab         1         8       4        1 |
      |-------------------------------------------------|
      |     Punjab         1         9       5        1 |
      |     Punjab         1        10       5        1 |
      |     Punjab         1        11       5        1 |
      |     Punjab         1        12       5        1 |
      |     Punjab         1        13       5        1 |
      |-------------------------------------------------|
      |     Punjab         1        14       6        1 |
      |     Punjab         1        15       6        1 |
      |     Punjab         1        16       6        1 |
      |     Punjab         1        17       6        1 |
      |-------------------------------------------------|
      |     Punjab         1        18       7        1 |
      |     Punjab         1        19       7        1 |
      |     Punjab         1        20       7        1 |
      |     Punjab         1        21       7        1 |
      |-------------------------------------------------|
      |     Punjab         1        22       8        1 |
      |     Punjab         1        23       8        1 |
      |     Punjab         1        24       8        1 |
      |     Punjab         1        25       8        1 |
      |     Punjab         1        26       8        1 |
      |-------------------------------------------------|
      |     Punjab         1        27       9        1 |
      |     Punjab         1        28       9        1 |
      |     Punjab         1        29       9        1 |
      |     Punjab         1        30       9        1 |
      |-------------------------------------------------|
      |     Punjab         1        31      10        1 |
      |     Punjab         1        32      10        1 |
      |     Punjab         1        33      10        1 |
      |     Punjab         1        34      10        1 |
      |     Punjab         1        35      10        1 |
      |-------------------------------------------------|
      |     Punjab         1        36      11        1 |
      |     Punjab         1        37      11        1 |
      |     Punjab         1        38      11        1 |
      |     Punjab         1        39      11        1 |
      |-------------------------------------------------|
      |     Punjab         1        40      12        1 |
      |     Punjab         1        41      12        1 |
      |     Punjab         1        42      12        1 |
      |     Punjab         1        43      12        1 |
      |-------------------------------------------------|
      |     Punjab         1        44      13        1 |
      |     Punjab         1        45      13        1 |
      |     Punjab         1        46      13        1 |
      |     Punjab         1        47      13        1 |
      |     Punjab         1        48      13        1 |
      +-------------------------------------------------+
    Last edited by Hemanshu Kumar; 10 Jul 2023, 03:20.

    Comment


    • #3
      Thank you, Hemanshu Kumar. That worked perfectly!

      Comment

      Working...
      X