Stata 循环子组

Stata Loop Over Subgroups

use https://stats.idre.ucla.edu/stat/stata/seminars/svy_stata_intro/strsrs, clear
svyset [pweight = api00] * we can pretend api00 equals to the weight variable*
tab sch_wide
svy: tab sch_wide
* attempt 1
svy: tab sch_wide, over(both)
* it gives error
* attempt 2
svy, subpop(both): tab sch_wide
* it works but does not give output for all values of 'both'
* i want this:
svy: tab sch_wide if both == 1
svy: tab sch_wide if both == 2
*without having to specify the if both command.

假设您有一些数据,您希望对其进行加权比例,并且希望在子组上进行加权。如何在不指定 if 命令的情况下做到这一点?我尝试了上面所示的两件事,但它们没有按预期工作。一个给出错误,另一个不产生所需的输出。换句话说,我想知道是否以及如何在组上循环加权制表。

除非您有其他不想使用 if 的根本原因,否则我建议采用以下方法,该方法可灵活用于 both.

的任意数量级别
levelsof both

foreach value in `r(levels)' {
    svy: tab sch_wide if both == `value'
}