Announcement

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

  • Creating variable that captures a range of time

    Hello all,

    I need assistance with creating a variable that captures a range of days. Participants come for monthly visits over a period of 6 months.Exact days are the number of days between enrolment to study and a specific date. We are carrying out multiple evaluations however, we are giving room of ±7 days. Hence, I went ahead to create a variable that subtracts 7 days from exact date (days_minus7) and another that adds 7 days (days_plus7).
    The range of days per visit are:
    Month 1 21 - 35
    Month 2 49 - 63
    Month 3 77 - 91
    Month 4 105 - 119
    Month 5 133 - 147
    Month 6 161 - 175
    How can I create a variable that captures the range of days as shown in the above? Please see sample data below.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float PID long Visit float(exact_days days_minus7 days_plus7)
     1 1  97  90 104
     1 2  97  90 104
     1 3  97  90 104
     2 1  39  32  46
     2 1  39  32  46
     2 1  39  32  46
     2 1  39  32  46
     3 1  32  25  39
     3 1  32  25  39
     3 1  32  25  39
     3 1  32  25  39
     4 8  21  14  28
     5 1 133 126 140
     5 2 133 126 140
     5 4 133 126 140
     5 3 133 126 140
     6 1 134 127 141
     6 1 134 127 141
     6 3 134 127 141
     6 4 134 127 141
     6 1 134 127 141
     6 1 134 127 141
     6 2 134 127 141
     7 3 113 106 120
     7 1 113 106 120
     7 4 113 106 120
     7 1 113 106 120
     7 1 113 106 120
     7 1 113 106 120
     7 2 113 106 120
     8 6 327 320 334
     8 1 327 320 334
     8 2 327 320 334
     8 4 327 320 334
     9 2 110 103 117
     9 1 110 103 117
     9 3 110 103 117
    10 1 110 103 117
    10 4 110 103 117
    10 2 110 103 117
    10 3 110 103 117
    11 1  40  33  47
    12 3 137 130 144
    12 4 137 130 144
    12 2 137 130 144
    12 1 137 130 144
    13 8   8   1  15
    14 1  40  33  47
    15 3 159 152 166
    15 2 159 152 166
    15 5 159 152 166
    15 1 159 152 166
    15 4 159 152 166
    16 6 173 166 180
    16 3 173 166 180
    16 2 173 166 180
    16 1 173 166 180
    16 1 173 166 180
    16 5 173 166 180
    16 4 173 166 180
    17 5 166 159 173
    17 4 166 159 173
    17 1 166 159 173
    17 1 166 159 173
    17 2 166 159 173
    17 3 166 159 173
    18 1  54  47  61
    19 3  61  54  68
    19 2  61  54  68
    19 1  61  54  68
    20 2 166 159 173
    20 4 166 159 173
    20 3 166 159 173
    20 1 166 159 173
    20 5 166 159 173
    20 1 166 159 173
    21 4 117 110 124
    21 2 117 110 124
    21 3 117 110 124
    21 1 117 110 124
    21 1 117 110 124
    21 1 117 110 124
    22 1 104  97 111
    22 2 104  97 111
    22 3 104  97 111
    23 2  75  68  82
    23 1  75  68  82
    24 3 169 162 176
    24 2 169 162 176
    24 4 169 162 176
    24 1 169 162 176
    24 1 169 162 176
    24 5 169 162 176
    24 1 169 162 176
    25 1  97  90 104
    25 3  97  90 104
    25 2  97  90 104
    26 1  75  68  82
    27 1  34  27  41
    28 1 179 172 186
    end
    label values Visit Visit
    label def Visit 1 "Month1", modify
    label def Visit 2 "Month2", modify
    label def Visit 3 "Month3", modify
    label def Visit 4 "Month4", modify
    label def Visit 5 "Month5", modify
    label def Visit 6 "Month6", modify

  • #2
    I'm not sure I understand what you want. I think it is this: create a variable, let's call it range, taking on values 1 through 6. It's value will be n if the interval from days_minus7 to days_plus7 overlaps the interval specified as range of days for Month n in your original post. If I have that correct, the following code does it:
    Code:
    gen range = .
    forvalues i = 1/6 {
        local lb = 28*`i'-7
        local ub = 28*`i'+7
        replace range = `i' if min(`ub', days_plus7) >= max(`lb', days_minus7)
    }
    A couple of caveats. The variable in your example data, Visit, is labeled as Month1 to Month 8, but it does not correspond well at all with the values of exact_days and the ranges defined. I really don't understand what that variable Visit represents.

    Perhaps more importantly, there are some instances in your data where the values of exact_days, days_minus7, and days_plus7 do not fall anywhere at all within any of the ranges of dates you specified. The code above returns missing values for range in those observations.

    Comment


    • #3
      Thank you so much Clyde Schechter. This is definitely what I needed. And you are absolutely right that I do have missing values. However, this is helpful in easily identifying outliers. For clarity, the "Visit" variable is one I created to indicate the participant visit and limited it to Months 1 to 6, so the 8 in the data was the screening visit.

      Comment

      Working...
      X