我有1200
个患者,我想在4
个随机组和 5
个区块中进行分配。
我可以如下创建组:
generate groups=1
replace groups=2 if id>300
replace groups=3 if id>600
replace groups=4 if id>900
但是,有没有一种方法可以使这些组成块随机出现?
答案 0 :(得分:1)
您可以首先使用seq()
命令的egen
函数来生成组和块:
clear
set obs 1200
egen groups = seq(), from(1) to(4)
egen blocks = seq(), block(5)
然后,您需要在块内的随机变量上对数据进行排序:
generate random = runiform()
sort blocks (random)
结果(前十个观察结果):
list in 1/10
+----------------------------+
| groups blocks random |
|----------------------------|
1. | 3 1 .0454974 |
2. | 4 1 .4462283 |
3. | 1 1 .5405352 |
4. | 1 1 .6910226 |
5. | 2 1 .7799934 |
|----------------------------|
6. | 2 2 .0061428 |
7. | 4 2 .0816054 |
8. | 2 2 .3310352 |
9. | 3 2 .4107759 |
10. | 1 2 .6661102 |
+----------------------------+
此处提供的解决方案是我在previous answer中提出的第一个解决方案的扩展。