Announcement

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

  • Counting observations for a household id split into multiple observations

    Hello
    I have a household data with a01 as the household id. Plotid represents the number of farming plot that household owns. h2_01 denotes the irrigation methods (labels below). I need to find out as to how many households are using rainfed as irrigation source (h2_01 = 1). One household can have multiple plots and can rely on rains for all those plots. I want to count the multiple plot observations as just "one" so as to get the number of farmers relying on rain.
    I tried using contract and bysort command but that gives me the count for all plots of a household. Eg, if a01 = 4 has two different plotids (2 & 4) and it depends on rains (h2_01 =1 ) for both, I just want to count h2_01 for a01 = 4 only once. Please help.



    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int a01 float plotid byte h2_01
     1   2 1
     1   3 3
     1   3 3
     3   2 2
     3   3 2
     3   5 1
     4   2 1
     4   4 1
     4   5 2
     4   7 2
     5   2 3
     5   2 3
     5   3 1
     6   2 3
     7   2 1
     9   2 3
    10   3 6
    10   7 3
    end
    label values h2_01 h2_01
    label def h2_01 1 "rainfed", modify
    label def h2_01 2 "river", modify
    label def h2_01 3 "canal", modify
    label def h2_01 4 "pond", modify
    label def h2_01 6 "canal irrigation", modify
    label def h2_01 7 "groundwater", modify

  • #2
    Saini:
    you may want to try something along the following lines:
    Code:
    . egen group=group( a01 h2_01) if h2_01==1
    
    
    . egen tag=tag( group)
    
    . replace tag= group if tag==1
    
    
    . tab tag if tag!=0
    
     tag(group) |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              1 |          1       20.00       20.00
              2 |          1       20.00       40.00
              3 |          1       20.00       60.00
              4 |          1       20.00       80.00
              5 |          1       20.00      100.00
    ------------+-----------------------------------
          Total |          5      100.00
    
    .
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Hello Carlo

      That worked! I require a little more help.
      I am trying to estimate the number of farmers who rely on rainfed over the total number of farmers in the dataset. As we can say, a01 == 5 does not rely on rainfed. There are multiple observations to each a01 (farmer). When I try to count the number of total farmers (bys a01 : egen farmer = count(a01)), it gives me the total by multiple obs (say, 3 for a01==5). I understand that I am using a wrong command.
      Is there a way to use tag command to figure out the total number of farmers? Or any other command that helps?

      Thanks
      Smriti

      Comment


      • #4
        Hey Carlo!

        I figured it out via the tag command. Just removed the rainfed condition from there and used the same command to calculate the number of farmers.

        Thanks
        Smriti

        Comment

        Working...
        X