Announcement

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

  • loop with rowtotal across categorical data

    Dear All,

    I have several categorical variables for each member of the hh indicating whether the person leave in the house within the farm, or close to the farm etc.
    I would like to construct new variables counting the number of person leaving in the household with the interviewed. That is sum-up to q26_1_6==1 to q26_@_6==1, if q26_1_6==2 then sum all the q26_@_6==2 and so on.

    I tried this:
    Code:
    foreach var of varlist q26_1_6 q26_2_6 q26_3_6 q26_4_6 q26_5_6 q26_6_6 q26_7_6 q26_8_6 q26_9_6 q26_10_6 q26_11_6 q26_12_6 {
    forvalues j=1/4 {
    egen hh_member`j'=rowtotal (`var') if `var'==`j'
    
    }
    }
    But it does not work.
    I copy a part of the dataset. Any help?
    thanks in advance


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(q26_1_6 q26_2_6 q26_3_6 q26_4_6 q26_5_6 q26_6_6 q26_7_6 q26_8_6 q26_9_6 q26_10_6 q26_11_6 q26_12_6)
    1 1 . . 3 . . . 1 4 . .
    1 1 . . 2 . . . 1 . . .
    1 1 . . . . . . 2 . . .
    1 1 . . 2 . . . 2 3 . .
    1 1 . . 2 . . . 1 . . .
    1 . . 1 2 . . . . . . .
    1 1 . . . . . . 4 . . .
    2 . . . 4 . . . . . . .
    1 1 . . 2 2 . . 1 . . .
    1 1 . . 2 . . . 3 . . .
    1 1 . . 4 4 . . 3 3 . .
    1 1 . . 2 4 . . 1 . . .
    1 1 . . 4 . . . 4 . . .
    1 . 2 . . . . . . . . .
    1 . . . . . . . 1 . . .
    1 . 1 . . . . . . . . .
    1 . 1 . . . . . . . . .
    1 . 2 . . . . . . . . .
    1 . . . 1 . . . . . . .
    1 . 1 . . . . . . . . .
    2 . 1 . . . . . . . . .
    1 . 1 . . . . . . . . .
    1 . . . . . . . . . . .
    1 . 2 . . . . . . . . .
    1 . . . . . . . . . . .
    1 . . . . . . . . . . .
    1 . . . . . . . . . . .
    1 1 . . . . . . 4 1 . .
    1 . 1 . . . . . . . . .
    1 1 . . . . . . . . . .
    1 . 2 . . . . . . . . .
    1 . 2 . . . . . . . . .
    1 . . . . . . . . . . .
    1 1 . . . . . . 1 . . .
    1 . . . . . . . . . . .
    1 . . . . . . . . . . .
    1 1 1 . . . . . . . . .
    1 1 . . . . . . 1 . . .
    1 1 . . . . . . 2 1 . .
    1 1 . . . . . . 1 1 . .
    1 . . . . . . . 2 2 . .
    1 . . . . . . . 2 1 . .
    1 1 . . . . . . . . . .
    1 1 . . . . . . . . . .
    1 1 . . . . . . . . . .
    1 1 . . . . . . 1 1 . .
    1 1 . . . . . . 1 1 . .
    1 1 . . . . . . . . . .
    1 1 . . . . . . . . . .
    1 . . . 1 . . . . . . .
    1 1 . . . . . . 1 . . .
    1 1 2 . . . . . . . . .
    1 . 2 . . . . . . . . .
    1 . 1 . . . . . . . . .
    1 . . . . . . . 2 . . .
    1 1 1 . . . . . . . . .
    1 1 . . . . . . 1 4 . .
    1 1 . . . . . . 1 . . .
    1 . 1 . . . . . . . . .
    1 1 . . . . . . 1 1 . .
    1 1 . . . . . . 1 1 . .
    1 . . . . . . . . . . .
    1 . . . . . . . . . . .
    1 . . . . . . . 2 3 . .
    1 1 . . . . . . . . . .
    1 1 2 . . . . . . . . .
    1 1 1 1 . . . . . . . .
    1 1 . . . . . . . . . .
    1 . 1 . . . . . . . . .
    1 . . . . . . . . . . .
    1 . 2 . . . . . . . . .
    1 1 . . . . . . . . . .
    1 . . . . . . . . . . .
    1 . 1 . . . . . . . . .
    1 . . . 1 . . . . . . .
    1 . . . . . . . . . . .
    1 1 . . . . . . 1 1 . .
    1 . . . . . . . 1 1 . .
    1 . . . . . . . 1 1 . .
    1 1 . . . . . . 2 . . .
    1 1 . . . . . . . . . .
    1 . . . . . . . . . . .
    1 1 . . . . . . 1 . . .
    1 . . . . . . . 2 . . .
    1 1 . . . . . . . . . .
    1 . . . 1 . . . . . . .
    1 . 1 . . . . . . . . .
    1 . . . . . . . . . . .
    1 . . . . . . . 2 . . .
    1 1 . . . . . . 1 1 . .
    1 . . . . . . . 2 1 . .
    2 2 . . . . . . 2 . . .
    1 1 . . . . . . 1 1 . .
    1 . . . . . . . . . . .
    1 1 . . 4 . . . . . . .
    1 1 . . 4 . . . 2 . . .
    1 1 . . . . . . 4 4 4 .
    1 1 . . 1 . . . 1 . . .
    1 2 . . 2 . . . 4 . . .
    2 2 . . 4 . . . 2 . . .
    end
    label values q26_1_6 q26_1_6
    label def q26_1_6 1 "home within the farm", modify
    label def q26_1_6 2 "home close to the farm", modify
    label values q26_2_6 q26_2_6
    label def q26_2_6 1 "home within the farm", modify
    label def q26_2_6 2 "home close to the farm", modify
    label values q26_3_6 q26_3_6
    label def q26_3_6 1 "home within the farm", modify
    label def q26_3_6 2 "home close to the farm", modify
    label values q26_4_6 q26_4_6
    label def q26_4_6 1 "home within the farm", modify
    label values q26_5_6 q26_5_6
    label def q26_5_6 1 "home within the farm", modify
    label def q26_5_6 2 "home close to the farm", modify
    label def q26_5_6 3 "home in a town/village easy to reach from the farm", modify
    label def q26_5_6 4 "further residences", modify
    label values q26_6_6 q26_6_6
    label def q26_6_6 2 "home close to the farm", modify
    label def q26_6_6 4 "further residences", modify
    label values q26_7_6 q26_7_6
    label values q26_8_6 q26_8_6
    label values q26_9_6 q26_9_6
    label def q26_9_6 1 "home within the farm", modify
    label def q26_9_6 2 "home close to the farm", modify
    label def q26_9_6 3 "home in a town/village easy to reach from the farm", modify
    label def q26_9_6 4 "further residences", modify
    label values q26_10_6 q26_10_6
    label def q26_10_6 1 "home within the farm", modify
    label def q26_10_6 2 "home close to the farm", modify
    label def q26_10_6 3 "home in a town/village easy to reach from the farm", modify
    label def q26_10_6 4 "further residences", modify
    label values q26_11_6 q26_11_6
    label def q26_11_6 4 "further residences", modify
    label values q26_12_6 q26_12_6

  • #2
    Your code doesn't make sense to me. The outer loop is over variables and the inner loop over 1 to 4.

    So, you're looping such that the first call is

    Code:
    egen hh_number1 = rowtotal(q26_1_6) if q26_1_6 == 1
    But the row total of one variable is just that variable; hence the syntax is legal but the calculation gives you only another version of data you have. .

    You will get problems with the second variable and the first category when

    Code:
    egen hh_number1 = rowtotal(q26_2_6) if q26_2_6 == 1
    will fail because the variable already exists.

    I think what you want is quite different.

    Code:
    forval j = 1/4 {
        egen hh_number`j' = anycount(q26_*_6), value(`j')
    }
    It's not material to the problem, but I guess leave is live, leaving is living.

    Comment


    • #3
      Thanks Nick!! Indeed this is what i was trying to do. I did not know the command anycount very useful...and yes it is living |

      Comment


      • #4
        It's easy to overlook that function, under the egen command.

        The egen functions anycount(), anymatch(), anyvalue() were added in Stata 7 and were community-contributed before that (under different names).

        Comment

        Working...
        X