Hi,

I want to calculate average of a variable X for all households for each 200 categories on variable Y.

By this I mean, bysort Y: tabstat mean(X) by (household) like of thing.

I cannot use 'by' and 'bysort' in one command, therefore, I am looking for a foreach loop command to perform the calculations.

An hypothetical data is as follows
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(x y Householdno)
1 1 1
2 1 2
3 1 3
4 1 4
5 1 5
3 2 1
4 2 2
5 2 3
9 2 4
3 2 5
. . .
end