Announcement

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

  • Calculating the cumulative freq of a variable

    Hi!

    I had previously asked a question around generating the frequency of a variable on here and was introduced to the 'contract' command which is super useful!

    I'm stuck on another issue and was hoping to get some input from people here

    I have attached a snippet of my data set below, to be brief; this is a dataset that gives us information on each employee (empcode), belonging to a certain unit and their vaccination status (1 being fully vaccinated) by WEEK. What I want to generate, is the cumulative number of people vaccinated in a unit by week. Is there a way I could generate this variable?

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long empcode float week str3 factory float year str12 unit float fullyvaccinated
    10000001 20 "F01" 2020 "F01" 1
    10000001 31 "F01" 2020 "F01" 1
    10000001 48 "F01" 2020 "F01" 1
    10000001  6 "F01" 2020 "F01" 1
    10000001 35 "F01" 2020 "F01" 1
    10000001 35 "F01" 2021 "F01" 1
    10000001 29 "F01" 2021 "F01" 1
    10000001 52 "F01" 2020 "F01" 1
    10000001 16 "F01" 2020 "F01" 1
    10000001 39 "F01" 2020 "F01" 1
    10000001 12 "F01" 2021 "F01" 1
    10000001 13 "F01" 2020 "F01" 1
    10000001 28 "F01" 2021 "F01" 1
    10000001 18 "F01" 2020 "F01" 1
    10000001  5 "F01" 2021 "F01" 1
    10000001 19 "F01" 2020 "F01" 1
    10000001 47 "F01" 2020 "F01" 1
    10000001 43 "F01" 2020 "F01" 1
    10000001 33 "F01" 2020 "F01" 1
    10000001 40 "F01" 2020 "F01" 1
    10000001 15 "F01" 2021 "F01" 1
    10000001 29 "F01" 2020 "F01" 1
    10000001 27 "F01" 2021 "F01" 1
    10000001 24 "F01" 2020 "F01" 1
    10000001  8 "F01" 2020 "F01" 1
    10000001  5 "F01" 2020 "F01" 1
    10000001 10 "F01" 2020 "F01" 1
    10000001 11 "F01" 2020 "F01" 1
    10000001 12 "F01" 2020 "F01" 1
    10000001 14 "F01" 2020 "F01" 1
    10000001  7 "F01" 2020 "F01" 1
    10000001 15 "F01" 2020 "F01" 1
    10000001  1 "F01" 2020 "F01" 1
    10000001 41 "F01" 2020 "F01" 1
    10000001 36 "F01" 2020 "F01" 1
    10000001 51 "F01" 2020 "F01" 1
    10000001 32 "F01" 2020 "F01" 1
    10000001 38 "F01" 2020 "F01" 1
    10000001 25 "F01" 2020 "F01" 1
    10000001 42 "F01" 2020 "F01" 1
    10000001  8 "F01" 2021 "F01" 1
    10000001 24 "F01" 2021 "F01" 1
    10000001 23 "F01" 2020 "F01" 1
    10000001 26 "F01" 2021 "F01" 1
    10000001  1 "F01" 2021 "F01" 1
    10000001  6 "F01" 2021 "F01" 1
    10000001 46 "F01" 2020 "F01" 1
    10000001 10 "F01" 2021 "F01" 1
    10000001 34 "F01" 2021 "F01" 1
    10000001 18 "F01" 2021 "F01" 1
    10000001 27 "F01" 2020 "F01" 1
    10000001  9 "F01" 2020 "F01" 1
    10000001 22 "F01" 2020 "F01" 1
    10000001 14 "F01" 2021 "F01" 1
    10000001 17 "F01" 2021 "F01" 1
    10000001 20 "F01" 2021 "F01" 1
    10000001 33 "F01" 2021 "F01" 1
    10000001 44 "F01" 2020 "F01" 1
    10000001 13 "F01" 2021 "F01" 1
    10000001 26 "F01" 2020 "F01" 1
    10000001 16 "F01" 2021 "F01" 1
    10000001 30 "F01" 2021 "F01" 1
    10000001 23 "F01" 2021 "F01" 1
    10000001 21 "F01" 2021 "F01" 1
    10000001 34 "F01" 2020 "F01" 1
    10000001 19 "F01" 2021 "F01" 1
    10000001 32 "F01" 2021 "F01" 1
    10000001 30 "F01" 2020 "F01" 1
    10000001  3 "F01" 2020 "F01" 1
    10000001  4 "F01" 2021 "F01" 1
    10000001 28 "F01" 2020 "F01" 1
    10000001 31 "F01" 2021 "F01" 1
    10000001 21 "F01" 2020 "F01" 1
    10000001 45 "F01" 2020 "F01" 1
    10000001  4 "F01" 2020 "F01" 1
    10000001 25 "F01" 2021 "F01" 1
    10000001 17 "F01" 2020 "F01" 1
    10000001 22 "F01" 2021 "F01" 1
    10000001  9 "F01" 2021 "F01" 1
    10000001 11 "F01" 2021 "F01" 1
    10000001 37 "F01" 2020 "F01" 1
    10000001 49 "F01" 2020 "F01" 1
    10000001  7 "F01" 2021 "F01" 1
    10000001 50 "F01" 2020 "F01" 1
    10000001  2 "F01" 2021 "F01" 1
    10000001  3 "F01" 2021 "F01" 1
    10000001  2 "F01" 2020 "F01" 1
    10000002 10 "F01" 2020 "F01" 1
    10000002 41 "F01" 2020 "F01" 1
    10000002 36 "F01" 2021 "F01" 1
    10000002 17 "F01" 2020 "F01" 1
    10000002 20 "F01" 2020 "F01" 1
    10000002 23 "F01" 2021 "F01" 1
    10000002  3 "F01" 2021 "F01" 1
    10000002 34 "F01" 2020 "F01" 1
    10000002 36 "F01" 2020 "F01" 1
    10000002 28 "F01" 2020 "F01" 1
    10000002 28 "F01" 2021 "F01" 1
    10000002  6 "F01" 2021 "F01" 1
    10000002  5 "F01" 2020 "F01" 1
    end


  • #2
    contract also has a cumulative frequency option. See also cumul.

    Comment


    • #3
      I realized that. I used contract though but I think I'm doing something wrong? I am not able to generate the correct cum freq!

      This is what i'm doing

      Code:
       contract unit week if fullyvaccinated == 1, cfreq(cfreq_num)
      Essentially I'm trying to find the cumulative number of employees (who have been vaccinated) for each unit by the week
      Last edited by Brendan Clark; 16 May 2022, 14:10.

      Comment


      • #4
        This could point you in a useful direction. What you don't discuss about your data, but you should consider, is the intermittent nature of how employees enter/leave the unit on a weekly basis. Since this is a factory, maybe some of these are part-time workers and this is sensible. I assume this is the case.

        I start with converting your weekly dates into a Stata weekly variable. This is not strictly necessary, but it is helpful.
        Code:
        gen int weekly = weekly(string(year)+"w"+string(week), "YW")
        format weekly %tw
        One approach
        Code:
        bys unit weekly : egen n_vaccinated = total(fullyvaccinated)
        bys unit weekly : keep if _n==1
        keep unit weekly n_vaccinated
        A simpler approach

        Code:
        contract unit weekly

        Comment


        • #5
          As a side detail,but one that could bite, Stata weeks are idiosyncratic and may not match yours. If you have any observations with week 53 in your data, you certainly don't have Stata weeks.

          Stata weeks always start on 1 January as the first day of week 1 in that year.

          Stata weeks always end on 31 December in week 52 with 8 or 9 days in that last week.

          Comment

          Working...
          X