Hello everyone,

I want to calculate the percentage of migrants in each district. I tried the codes below but I keep getting error messages "invalid 'by'". Thank you for the help.
Code:
egen iv = total (migrant), if migrant => 1, by( district)
egen iv = total (migrant), if migrant >= 1, by( district)
egen iv = total (migrant), if migrant >= 1, by(district)

Code:
tab district migrant

           |      migrant
    district |         0          1 |     Total
-----------+----------------------+----------
         1 |        65         79 |       144 
         2 |        72         60 |       132 
         3 |        24         24 |        48 
         4 |        15          9 |        24 
         5 |        50         22 |        72 
         6 |        60         84 |       144 
         7 |        46         38 |        84 
         8 |        58         86 |       144 
         9 |        49         47 |        96 
        10 |        57         39 |        96 
        11 |        28         20 |        48 
        12 |        27         21 |        48 
        13 |        92         76 |       168 
        14 |        16          8 |        24 
        15 |        18          6 |        24 
        16 |        53         43 |        96 
        17 |        70         74 |       144 
        18 |        26         22 |        48 
        19 |        31         17 |        48 
        20 |        62         34 |        96 
        21 |        51         45 |        96 
        22 |        46         50 |        96 
-----------+----------------------+----------
     Total |     1,016        904 |     1,920

Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input byte district float migrant
1 0
6 0
1 1
1 1
6 0
1 1
6 1
1 1
6 1
6 1
1 1
6 1
1 1
1 0
6 0
6 1
6 1
1 1
6 1
1 0
6 1
1 1
6 0
1 0
6 0
6 0
6 0
6 1
6 1
6 1
6 0
6 0
6 0
6 1
1 1
6 0
1 0
6 1
1 0
6 1
1 1
6 0
1 1
6 1
6 1
1 1
1 1
6 0
1 1
6 1
1 0
6 1
1 0
6 0
1 0
6 0
6 0
1 1
6 1
6 0
6 0
6 1
6 1
6 1
6 1
6 0
6 0
6 1
6 1
1 1
6 1
1 1
6 1
1 1
6 0
6 1
1 1
6 1
1 1
6 1
1 0
6 1
6 0
1 0
1 0
1 1
1 1
1 1
1 1
1 1
1 1
1 0
1 0
1 0
1 0
1 0
1 1
1 0
1 0
1 0
end