. foreach l of local levels {
2. preserve
3. keep if region == "`l'"
4.
. quietly table `A' `B'
5. local N = r(N)
6.
. egen tag = tag(`A')
7. egen sum = sum(tag)
8. local p = sum[1]
9. drop sum tag
---
I don't like how the blank lines are numbered and the next line is
given the "." To me, this seems better:
---
. foreach l of local levels {
2. preserve
3. keep if region == "`l'"
.
4. quietly table `A' `B'
5. local N = r(N)
.
6. egen tag = tag(`A')
7. egen sum = sum(tag)
8. local p = sum[1]
9. drop sum tag
---