I am trying to generate a table of the below format.

Below is my data:
Below is my code so far:
[CODE]
* Step 1: Count the number of unique structures per cluster, LGA, and state
egen num_structures = count(uniquestrucid), by(state_name exposure lga_name clustercode)
* Step 2: Count the number of unique households per cluster, LGA, and state
egen num_households = count(uniquehhid), by(state_name exposure lga_name clustercode)
* Step 3: Count the number of unique females aged 15-19 per cluster, LGA, and state
egen num_females_15_19 = count(uniquegirlid), by(state_name exposure lga_name clustercode)
* Step 4: Count the number of **eligible females (15-19, not pregnant, and living there)**
egen num_eligible_females = count(uniquegirlid) if femalepregnant == 0 & femalelivinghere == 1, by(state_name exposure lga_name clustercode)
* Step 5: Collapse data to keep only unique counts at the required levels
collapse (sum) num_structures num_households num_females_15_19 num_eligible_females, by(state_name exposure lga_name clustercode)
* Step 6: Sort data to maintain table structure
sort state_name exposure lga_name clustercode
* Step 7: Display the table
list state_name exposure lga_name clustercode num_structures num_households num_females_15_19 num_eligible_females, sepby(state_name exposure lga_name)
[CODE]
The count egen function is not counting unqiuely. I tried nval and unique to no avail.
Below is my data:
Code:
* Example generated by -dataex-. For more info, type help dataex clear input int clustercode str8 uniquestrucid str10 uniquehhid str12 uniquegirlid byte(femalepregnant femalelivinghere) str8 state_name str10 lga_name str12 exposure 2224 "2224_33" "2224_33_1" "" . . "KADUNA" "SANGA" "Control" 2229 "2229_49" "2229_49_43" "" . . "KADUNA" "SANGA" "Control" 2229 "2229_5" "" "" . . "KADUNA" "SANGA" "Control" 2236 "2236_18" "2236_18_1" "2236_18_1_2" 1 1 "KADUNA" "SANGA" "Control" 2254 "2254_11" "2254_11_12" "2254_11_12_1" 0 1 "KADUNA" "SANGA" "Control" 2255 "2255_31" "" "" . . "KADUNA" "SANGA" "Control" 2256 "2256_26" "2256_26_15" "" . . "KADUNA" "SANGA" "Control" 2260 "2260_13" "2260_13_1" "2260_13_1_1" 0 1 "KADUNA" "SANGA" "Control" 2261 "2261_51" "" "" . . "KADUNA" "SANGA" "Control" 2261 "2261_31" "2261_31_1" "" . . "KADUNA" "SANGA" "Control" 2261 "2261_25" "2261_25_1" "2261_25_1_1" 0 1 "KADUNA" "SANGA" "Control" 2261 "2261_29" "2261_29_1" "" . . "KADUNA" "SANGA" "Control" 2262 "2262_32" "2262_32_22" "2262_32_22_1" 0 1 "KADUNA" "SANGA" "Control" 2262 "2262_35" "2262_35_25" "2262_35_25_3" 0 1 "KADUNA" "SANGA" "Control" 2263 "2263_21" "2263_21_16" "" . . "KADUNA" "SANGA" "Control" 2153 "2153_5" "" "" . . "KADUNA" "IGABI" "Intervention" 2156 "2156_39" "" "" . . "KADUNA" "IGABI" "Intervention" 2156 "2156_6" "2156_6_1" "2156_6_1_4" 0 1 "KADUNA" "IGABI" "Intervention" 2156 "2156_47" "2156_47_1" "2156_47_1_1" 0 1 "KADUNA" "IGABI" "Intervention" 2156 "2156_25" "2156_25_1" "" . . "KADUNA" "IGABI" "Intervention" 2156 "2156_61" "" "" . . "KADUNA" "IGABI" "Intervention" 2157 "2157_85" "2157_85_1" "2157_85_1_1" 0 1 "KADUNA" "IGABI" "Intervention" 2157 "2157_87" "2157_87_1" "2157_87_1_3" 0 1 "KADUNA" "IGABI" "Intervention" 2157 "2157_81" "2157_81_1" "" . . "KADUNA" "IGABI" "Intervention" 2157 "2157_27" "2157_27_1" "" . . "KADUNA" "IGABI" "Intervention" 2157 "2157_63" "" "" . . "KADUNA" "IGABI" "Intervention" 2157 "2157_82" "2157_82_1" "2157_82_1_1" 1 1 "KADUNA" "IGABI" "Intervention" 2158 "2158_16" "2158_16_1" "2158_16_1_1" 1 1 "KADUNA" "IGABI" "Intervention" 2160 "2160_36" "2160_36_6" "" . . "KADUNA" "IGABI" "Intervention" 2160 "2160_56" "2160_56_1" "2160_56_1_1" 0 1 "KADUNA" "IGABI" "Intervention" 2160 "2160_1" "2160_1_1" "2160_1_1_1" 0 1 "KADUNA" "IGABI" "Intervention" 2160 "2160_1" "2160_1_3" "" . . "KADUNA" "IGABI" "Intervention" 2161 "2161_40" "2161_40_1" "2161_40_1_1" 0 1 "KADUNA" "IGABI" "Intervention" 2161 "2161_36" "2161_36_2" "" . . "KADUNA" "IGABI" "Intervention" 2161 "2161_47" "2161_47_1" "2161_47_1_1" 0 1 "KADUNA" "IGABI" "Intervention" 2165 "2165_14" "2165_14_2" "" . . "KADUNA" "IGABI" "Intervention" 2165 "2165_8" "2165_8_2" "" . . "KADUNA" "IGABI" "Intervention" 2165 "2165_18" "2165_18_2" "" . . "KADUNA" "IGABI" "Intervention" 2165 "2165_5" "2165_5_3" "" . . "KADUNA" "IGABI" "Intervention" 2111 "2111_30" "2111_30_1" "" . . "KADUNA" "SABON_GARI" "Intervention" 2116 "2116_36" "" "" . . "KADUNA" "SABON_GARI" "Intervention" 2117 "2117_21" "2117_21_4" "2117_21_4_1" 0 1 "KADUNA" "SABON_GARI" "Intervention" 1104 "1104_76" "1104_76_1" "1104_76_1_2" 0 1 "NASARAWA" "DOMA" "Intervention" 1104 "1104_31" "1104_31_1" "" . . "NASARAWA" "DOMA" "Intervention" 1104 "1104_24" "1104_24_1" "1104_24_1_1" 0 1 "NASARAWA" "DOMA" "Intervention" 1104 "1104_47" "1104_47_1" "1104_47_1_2" 0 1 "NASARAWA" "DOMA" "Intervention" 1104 "1104_30" "" "" . . "NASARAWA" "DOMA" "Intervention" 1108 "1108_111" "1108_111_1" "1108_111_1_1" 0 1 "NASARAWA" "DOMA" "Intervention" 1108 "1108_49" "1108_49_1" "1108_49_1_2" 0 1 "NASARAWA" "DOMA" "Intervention" 1110 "1110_7" "1110_7_1" "" . . "NASARAWA" "DOMA" "Intervention" end label values femalepregnant femalepregnant label def femalepregnant 0 "No", modify label def femalepregnant 1 "Yes", modify label values femalelivinghere femalelivinghere label def femalelivinghere 1 "Yes", modify
[CODE]
* Step 1: Count the number of unique structures per cluster, LGA, and state
egen num_structures = count(uniquestrucid), by(state_name exposure lga_name clustercode)
* Step 2: Count the number of unique households per cluster, LGA, and state
egen num_households = count(uniquehhid), by(state_name exposure lga_name clustercode)
* Step 3: Count the number of unique females aged 15-19 per cluster, LGA, and state
egen num_females_15_19 = count(uniquegirlid), by(state_name exposure lga_name clustercode)
* Step 4: Count the number of **eligible females (15-19, not pregnant, and living there)**
egen num_eligible_females = count(uniquegirlid) if femalepregnant == 0 & femalelivinghere == 1, by(state_name exposure lga_name clustercode)
* Step 5: Collapse data to keep only unique counts at the required levels
collapse (sum) num_structures num_households num_females_15_19 num_eligible_females, by(state_name exposure lga_name clustercode)
* Step 6: Sort data to maintain table structure
sort state_name exposure lga_name clustercode
* Step 7: Display the table
list state_name exposure lga_name clustercode num_structures num_households num_females_15_19 num_eligible_females, sepby(state_name exposure lga_name)
[CODE]
The count egen function is not counting unqiuely. I tried nval and unique to no avail.

Comment