Announcement

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

  • Generating a dummy out of a date variable

    Hi,

    For my dataset, I'm trying to generate a variable for the Vaccination status of individuals based on the their dataset.

    My data consists of individual information for participants in a camp, subset by week. There is also the vaccination information for each of these individuals, namely, the date they received the first dose and date of second dose. From this, I want to create a dummy vaccination variable (which is 1 if an individual has received at least one vaccination dose).

    In the code below, I tried to generate this variable by extracting the week and generating a dummy based on this. So the vaccinated variable is one if the week in the cell is greater (or equal to) the week they received the first dose.

    I think there might be an easier/more fluid way to generate this dummy, so I wanted opinions on whether there is another way to this!

    Code:
    g week_vaccinated = week(datedose1)
    gen vaccinated = 1 if week >= week_vaccinated
    replace vaccinated = 0 if year == 2020 | vaccinated == . |week < week_vaccinated

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float week long empcode float(datedose1 datedose2 year)
     1 20149390 22540     . 2021
     1 20008408 22446 22545 2021
     2 20008408 22446 22545 2021
     2 20149390 22540     . 2021
     3 20149390 22540     . 2021
     3 20008408 22446 22545 2021
     4 20008408 22446 22545 2021
     4 20149390 22540     . 2021
     5 20008408 22446 22545 2021
     5 20149390 22540     . 2021
     6 20008408 22446 22545 2021
     6 20149390 22540     . 2021
     7 20149390 22540     . 2021
     7 20008408 22446 22545 2021
     8 20149390 22540     . 2021
     8 20008408 22446 22545 2021
     9 20149390 22540     . 2021
     9 20008408 22446 22545 2021
    10 20008408 22446 22545 2021
    10 20149390 22540     . 2021
    11 20008408 22446 22545 2021
    11 20149390 22540     . 2021
    12 20008408 22446 22545 2021
    12 20149390 22540     . 2021
    13 20149390 22540     . 2021
    13 20008408 22446 22545 2021
    14 20008408 22446 22545 2021
    14 20149390 22540     . 2021
    15 20008408 22446 22545 2021
    15 20149390 22540     . 2021
    16 20149390 22540     . 2021
    16 20008408 22446 22545 2021
    17 20149390 22540     . 2021
    17 20008408 22446 22545 2021
    18 20008408 22446 22545 2021
    18 20149390 22540     . 2021
    19 20008408 22446 22545 2021
    19 20149390 22540     . 2021
    20 20149390 22540     . 2021
    20 20008408 22446 22545 2021
    21 20008408 22446 22545 2021
    21 20149390 22540     . 2021
    22 20149390 22540     . 2021
    22 20008408 22446 22545 2021
    44 20008408 22446 22545 2020
    44 20110196 22446 22545 2020
    44 20149390 22540     . 2020
    45 20110196 22446 22545 2020
    45 20149390 22540     . 2020
    45 20008408 22446 22545 2020
    46 20110196 22446 22545 2020
    46 20008408 22446 22545 2020
    46 20149390 22540     . 2020
    47 20149390 22540     . 2020
    47 20110196 22446 22545 2020
    47 20008408 22446 22545 2020
    48 20008408 22446 22545 2020
    48 20149390 22540     . 2020
    48 20110196 22446 22545 2020
    49 20149390 22540     . 2020
    49 20110196 22446 22545 2020
    49 20008408 22446 22545 2020
    50 20149390 22540     . 2020
    50 20110196 22446 22545 2020
    50 20008408 22446 22545 2020
    51 20110196 22446 22545 2020
    51 20008408 22446 22545 2020
    51 20149390 22540     . 2020
    52 20008408 22446 22545 2020
    52 20110196 22446 22545 2020
    52 20149390 22540     . 2020
     1 20365003 22440     . 2021
     1 20359591 22440     . 2021
     2 20359591 22440     . 2021
     2 20365003 22440     . 2021
     3 20365003 22440     . 2021
     3 20359591 22440     . 2021
     4 20359591 22440     . 2021
     4 20365003 22440     . 2021
     5 20359591 22440     . 2021
     5 20365003 22440     . 2021
     6 20365003 22440     . 2021
     6 20359591 22440     . 2021
     7 20365003 22440     . 2021
     7 20359591 22440     . 2021
     8 20365003 22440     . 2021
     8 20359591 22440     . 2021
     9 20359591 22440     . 2021
     9 20365003 22440     . 2021
    10 20359591 22440     . 2021
    10 20365003 22440     . 2021
    11 20365003 22440     . 2021
    11 20359591 22440     . 2021
    12 20359591 22440     . 2021
    12 20365003 22440     . 2021
    13 20359591 22440     . 2021
    13 20365003 22440     . 2021
    14 20365003 22440     . 2021
    14 20359591 22440     . 2021
    15 20365003 22440     . 2021
    end
    format %td datedose1
    format %td datedose2



  • #2
    Brendan:
    as per your data excerpt (no vaccinated persons in 2021), the last line of your code should be amended a bit:
    Code:
    . replace vaccinated = 0 if year == 2021 & vaccinated == . & week < week_vaccinated
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment

    Working...
    X