Announcement

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

  • count group var if a condition is met

    Hello,

    I am using Stata 15.1 to analyse household survey data. I want to simply count the number of HHs with people between 15 and 60 years of age. I can get the total number of people meeting this condition by using: count if dage>15 & dage<60. Thanks much.

    Robert

  • #2
    You don't give a data example (do please read and act on https://www.statalist.org/forums/help#stata) but something like this illustrates technique

    Code:
    clear 
    input hhid age 
    1    60
    1    55 
    1    18 
    1    15  
    2    24
    2    20 
    end 
    
    egen any = max(inrange(age, 16, 59)), by(hhid) 
    egen tag = tag(hhid) 
    
    tab hhid if tag & any 
    
           hhid |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              1 |          1       50.00       50.00
              2 |          1       50.00      100.00
    ------------+-----------------------------------
          Total |          2      100.00
    
    di r(r) 
    2
    
    count if tag & any 
      2
    See the help for egen and https://www.stata.com/support/faqs/d...ble-recording/

    Comment


    • #3
      Super! Thanks much.

      Comment

      Working...
      X