Announcement

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

  • generating a variable for counting average entries

    Hi all, I had a small coding problem and was wondering if you can help me with it.
    I have the following data for workers and the entries they make. Each date is an entry
    Worker date
    worker_name1 11sep2022
    worker_name1 11sep2022
    worker_name1 12sep2022
    worker_name1 15sep2022
    I want to create a variable that counts the number of entries by each worker for the same day and another variable that gets the average entry per day so that :
    worker date no. of entries
    worker_name1 11sep2022 2
    worker_name1 12sep2022 1
    worker_name1 15sep2022 1
    -------------------------------------------------------------------------------------------------------------------------------
    worker average_entries per day
    worker_name1 1.3
    Thank you in advance
    Last edited by Girgies Yostina; 14 Mar 2023, 05:19.

  • #2
    Code:
    bysort worker date : gen freq = _N 
    egen tag = tag(worker date) 
    egen wanted = mean(cond(tag == 1, freq, .)), by(worker)
    See also https://www.stata-journal.com/articl...article=dm0055 esp. Section 9.

    But, but, but: Zeros aren't included. This is a mean over workers and days present, not days absent from the dataset.

    Comment

    Working...
    X