Announcement

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

  • 'Generate count=_n' when you want to skip the lines with the dots

    Hello!!

    I have a question and I would appreciate your help. I am trying to count the amount of each vaccine at a clinic in a particular month. So this is the command that I am using:
    by clinic vaccine_month vaccine_year, sort: gen bcg_count = _n if vaccine=1
    by clinic vaccine_month vaccine_year, sort: egen bcg_no = max(bcg_count)

    However, the if condition doesn't work. When vaccine 1 was not given it appears as a dot but somehow it still counts. For instance, it counts 1, ., 3 when it should be 1,.,2

    Please let me know if you know what I am doing wrong



  • #2
    Code:
    by clinic vaccine_month vaccine_year, sort: gen bcg_count = _n if vaccine=1
    is not legal as you need == not =.

    You need to sort on vaccine too, I guess. _n always means the observation number and that number isn't influenced by an if condition.

    Code:
    by clinic vaccine_month vaccine_year vaccine, sort: gen bcg_count = _n if vaccine==1
    To see the point differently, consider

    Code:
     
    gen long obsno = _n if _n == _N
    There is only one observation which is the last, but the new variable will contain the last observation number, regardless of the fact that it is the first and only observation satisfying that condition.

    Comment


    • #3
      Code:
      clear
      **first create some example/fake data:: 
      set obs 100
      g clinic = int(runiform()*5)
      g month  = int(1+runiform()*5)
      g year = 2000+int(runiform()*3)
      g vaccine = cond(rbinomial(1,.66)==1, 1, .)
      
      
      *******
      //note how this counts the missing vaccine as a seperate group/panel
      bys clinic month year (vaccine) : g bcg_count = _n  if vaccine ==1
      
      
      //you could use _N sorted on vaccine if you want the count of vaccine  by clinic-month-year panel: 
         **remove the if part below to get the count of non vaccine by panel group as well
      bys clinic month year vaccine : g bcg_count1 =  _N if vaccine==1
      
      // or just use egen to get the totals of vaccine (add up the 1's) by clinic-month-year
      // this one seems to most directly answer your original question (the way I interpret it anyways)
      bys clinic month year : egen bcg_count2 = total(vaccine)
      
       //note how these 2 are different solutions
       g x = 1 if bcg_count1 != bcg_count2
      Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

      Comment

      Working...
      X