Hello,

I spend too many hours trying to figure it out - and I know it should be an easy fix but I can't get it right. I have a household survey with 12249 households. Of these 3932 do not have a certain type of expenditure. I need to assign them an average expenditure value based on conditions. The average values are based on the rest of the household 12249-3932=8317.
I want a for-loop where for each observation (household) several conditions will be checked. If they are true average values of spends will be assigned based on conditions corresponding to another group of households. My result supposed to be a variable filled in with 3932 values and 8317 zeros (or no values).

So far I tried the following but it does not give me my result:

Code:
generate avg_dir_en_percap_new = .     * I'm generating a new variable in which the result will be stored 
        
quietly forval s=1/7 {                                          * for stratum (income levels) from 1 -lowest to 7 - highest
forval c = 1/12 {                                       * for cooking device - 12 cooking types
forval m = 1/4 {                                         * for number of meals per day
forval d= 1/74 {                                            * for number of districts: 74
      generate include = 1 if hh_dir_spends_status==0 &  stratum ==`s' & sec8q15 ==`c' & sec15_6 ==`m' & sec8q13<3 & dist==`d'            *hh_dir_spends_status is a variable which tells me if a household have or not spends on direct energy. If not then the average value needs to be assigned. This line suppose to select households that depending on certain conditions will have assigned different average. SO if they are from strtum 1, use cooking device 2 have one meal per day and are in distric 4 - they will have a different average than if they are from district 8 or eat 3 meals or use different cooking device. 
      egen avg = mean(include*percap_firewood_unweighted) if hh_dir_spends_status==1 &  stratum ==`s' & sec8q15 ==`c' & sec15_6 ==`m' &percap_firewood_unweighted > 0, by (dist) * for households that do have reported expenditures the average of spends needs to be calculated depending on a district they are from. The average should be based firstly on the district and also wiithin it will be different depending on stratum, type of cooking device etc. 
       replace avg_dir_en_percap_new = avg if include == 1 * here the average calculated in egen avg should be matched with include. 
       drop include avg
  } 
}
}
}
When I run it without the for-loop just for one of the possibilities, I get include which is variable with 0 and 1 depending if a condition is met or not, avg which is variable that calculated average depending on conditions in each district. I can get Stata to assign the avg value to the right household though as they are not in the same line.