我的数据格式如下:
Month Date Time1 Time2 Time3
Jan 01/01/2018 1234 2345 3456
Jan 02/01/2018 1234 2346 3457
Jan 03/02/2018 1234 2346 3145
Feb 03/02/2018 1234 2347 3458
Feb 04/02/2018 1235 2398 3456
March 04/03/2018 1235 2347 3456
March 01/03/2018 1236 2345 3456
March 02/03/2018 1234 2567 3897
Feb 02/02/2018 1236 2346 3478
我想随机选择观察值,以便从每个月和每个时隙中获得两个样本日期和数字。例如,对于Jan
,Time 1
,我想获取:01/01/2018
,1234
和02/01/2018
,1234
。
如何在Stata中做到这一点?
答案 0 :(得分:2)
您可以如下随机选择观测值:
clear
input str5 Month str10 Date Time1 Time2 Time3
Jan "01/01/2018" 1234 2345 3456
Jan "02/01/2018" 1234 2346 3457
Jan "03/02/2018" 1234 2346 3145
Feb "03/02/2018" 1234 2347 3458
Feb "04/02/2018" 1235 2398 3456
March "04/03/2018" 1235 2347 3456
March "01/03/2018" 1236 2345 3456
March "02/03/2018" 1234 2567 3897
Feb "02/02/2018" 1236 2346 3478
end
bysort Month (Time1): generate random = runiform()
bysort Month (Time1 random): keep if _n < 3
list, sepby(Month)
+-------------------------------------------------------+
| Month Date Time1 Time2 Time3 random |
|-------------------------------------------------------|
1. | Feb 03/02/2018 1234 2347 3458 .3576297 |
2. | Feb 04/02/2018 1235 2398 3456 .4004426 |
|-------------------------------------------------------|
3. | Jan 02/01/2018 1234 2346 3457 .2076905 |
4. | Jan 01/01/2018 1234 2345 3456 .5597356 |
|-------------------------------------------------------|
5. | March 02/03/2018 1234 2567 3897 .0286627 |
6. | March 04/03/2018 1235 2347 3456 .6889245 |
+-------------------------------------------------------+