在stata中的所有变量之间折叠



我正在尝试collapse数据集中的所有变量,如下所示。

date     number_of_patients   health_center      vaccinations
6/25/21  1                    healthcentername   1
6/18/21  2                    healthcentername   2
10/9/20  2                    healthcentername   1
10/2/20  2                    healthcentername   1
10/16/20 1                    healthcentername   1

我正试图通过计数到来按日期collapse

number_of_patients  healthcentername  vaccinations
8                   healthcentername  6

我正试图在所有的健康中心做到这一点,但如果不确定我想崩溃的具体变量,我似乎无法做到。不幸的是,这并不完全可行,因为我在数据帧中有3500个变量。

不知何故,您需要告诉Stata您希望由健康中心对哪些变量求和,但这并不意味着您需要键入所有变量。可以使用ds创建变量名列表。如果使用选项not,则ds将列出除您提到的变量名之外的所有变量名。像这样:

* Example generated by -dataex-. For more info, type help dataex
clear
input str8 date byte number_of_patients str16 health_center byte vaccinations
"6/25/21"  1 "healthcentername" 1
"6/18/21"  2 "healthcentername" 2
"10/9/20"  2 "healthcentername" 1
"10/2/20"  2 "healthcentername" 1
"10/16/20" 1 "healthcentername" 1
end
*List all variables but the one mentioned and store list in r(varlist)
ds date health_center, not
*Sum by health center all but the variables explicitly excluded above
collapse (sum) `r(varlist)' , by(health_center)

最新更新