我有1200
名患者要分配到4
个随机治疗组中。
我可以如下创建组:
generate groups=1
replace groups=2 if id>300
replace groups=3 if id>600
replace groups=4 if id>900
但是,如何使这些组中的患者分配随机?
答案 0 :(得分:1)
您只需要创建一个随机变量进行排序:
clear
set obs 1200
generate random = runiform()
sort random
然后,您可以使用seq()
命令的egen
功能:
egen groups = seq(), from(1) to(4)
tabulate groups
groups | Freq. Percent Cum.
------------+-----------------------------------
1 | 300 25.00 25.00
2 | 300 25.00 50.00
3 | 300 25.00 75.00
4 | 300 25.00 100.00
------------+-----------------------------------
Total | 1,200 100.00
不太直观的cond()
函数也可以使用:
generate groups = cond(_n <= 300, 1, cond(_n <= 600, 2, cond(_n <= 900, 3, 4)))