Announcement

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

  • Client level data- Count number of sessions per year

    Hi All,

    My organization offer sexual health classes to adolescent in rural communities. We have doing this for almost 5 years. We have offered many sessions per year. I would like to know how many session we have offered per year. So in year 1, we offered 2 session and in year 3 we offered 3 sessions. Below if a extract of my data. I was playing with the system variables -n and _N but I was not successful (by year: generate Nsession =session[ _N])
    ID Session Year
    100 1 1
    101 1 1
    102 2 1
    103 2 1
    104 3 2
    105 3 2
    106 4 2
    107 4 2
    108 5 2
    Thank you,
    Marvin

  • #2
    Marvin,
    I'm not completely clear with what you're after.
    However, I do hope that what follows may turn out helpful:
    Code:
    total Session, over(Year)
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      One keyword here is distinct. The number of distinct sessions per year is

      Code:
      egen tag = tag(Session Year)
      egen ndistinct = total(tag), by(Year)
      Here is another way to do it:

      Code:
      bysort Year Session : gen ndistinct = _n == 1
      by Year: replace ndistinct = sum(ndistinct)
      by Year: replace ndistinct = ndistinct[_N]
      Either way,

      Code:
      tabdisp Year, c(ndistinct)
      Some reading:

      Code:
      FAQ     . . . . . . . . . . . . . . . . . . .  Number of distinct observations
              . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox and G. Longton
              10/08   How do I compute the number of distinct observations?
                      http://www.stata.com/support/faqs/data-management/
                      number-of-distinct-observations/
      
      FAQ     . . . . . . . . .  Counting distinct strings across a set of variables
              . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
              11/06   How do I count the number of distinct strings
                      across a set of variables?
                      http://www.stata.com/support/faqs/data-management/
                      counting-distinct-strings/
      
      FAQ     . . . . . . . . . . . . . .  Calculating the number of distinct values
              . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
              9/06    How do I calculate the number of distinct
                      values seen so far?
                      http://www.stata.com/support/faqs/data-management/
                      calculating-number-of-distinct-values/
      
      SJ-12-2 dm0042_1  . . . . . . . . . . . . . . . . Software update for distinct
              (help distinct if installed)  . . . . . .  N. J. Cox and G. M. Longton
              Q2/12   SJ 12(2):352
              options added to restrict output to variables with a minimum
              or maximum of distinct values
      
      SJ-8-4  dm0042  . . . . . . . . . . . .  Speaking Stata: Distinct observations
              (help distinct if installed)  . . . . . .  N. J. Cox and G. M. Longton
              Q4/08   SJ 8(4):557--568
              shows how to answer questions about distinct observations
              from first principles; provides a convenience command

      Comment


      • #4
        Thank you Carlos and Nick. Nick: it works perfectly. I don't know why but I have trouble thinking about the logic when I deal with groups like this (not rows but columns). I will try to understand what you did. I will read the reading you suggested, as well.

        Thank you very much,
        Marvin

        Comment


        • #5
          The logic is this. We tag one observation in each session in each year with 1 and the others with 0. Then we add up the 1s over years. (We end up adding the 0s too but manifestly they make no difference.) We have a name for 1 + 1 + 1 + ...: it's counting! The egen function tag() is designed to tag just one of any number of repeated occurrences. I first wrote it and named it, but I was using ideas repeatedly talked about in the Stata world at the time.

          It's the same logic as would apply if you were asked "How many of your work colleagues have you seen today?". If you saw Marcello getting coffee, in the corridor, at his computer, that makes no difference: Marcello is just one colleague you saw today, not three. If you saw Maria once in her office, that is another colleague. Naturally, I don't know what your colleagues are called, but the logic in Stata is just that of counting anywhere. Don't count repeated occurrences more than once if you are counting individuals.
          Last edited by Nick Cox; 21 Apr 2015, 13:20.

          Comment


          • #6
            Thank for the explanation Nick! I was reading some of the suggested readings. One question, where can I find the dataset used in these readings, for example in the reading "How do I compute the number of distinct observations?" var rep78, foreign? Thanks!
            Last edited by Marvin Aliaga; 22 Apr 2015, 09:43.

            Comment


            • #7
              Code:
              sysuse auto

              Comment


              • #8
                Thanks!

                Comment

                Working...
                X