Dear All,
I am facing issues with running a loop to draw my sample by a certain proportion. What I wish to achieve is, for instance, in strata 8 draw 1 consumer randomly from the larger pool of consumers in strata 8. The code for the same is as below but I keep running into a syntax error for the loop.
local numlist 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
local strata 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 22 23 24 25 27 30 31 32 33 35 36 37 38 39 41 42 43 44 45 46 47 48 49 50 51 54 55 56 57 58 59 61 62 63 65 66 67 68 70 71 72 73 74 76 77 78 80 81 82 83 84 86 88 89 90 93 94 95 96 98 100 102 103 104 105 106 107 108 109 110 111 112 113 114 116 117 119 121 122 123 124 127 128 129
local proportion 1 1 2 2 9 26 1 1 2 5 8 36 13 10 9 19 1 2 18 22 2 5 8 5 2 1 22 1 1 1 14 7 4 6 2 22 7 8 5 7 13 2 18 2 36 12 1 8 2 4 4 10 1 4 3 33 2 2 2 1 1 1 1 1 1 1 1 2 4 10 7 2 8 2 22 3 36 1 2 2 1 1 2 8 24 10 2 1 1 9 4 3 2 5 2 3 1 2 1 2 3 1 2 1
set seed 12345
local seed_num = 12345
forvalue i=1/104 of local numlist {
local strata : word `i' of `strata'
local proportion: word `i' of `proportion'
preserve
keep if strata==`strata'
set seed `seed_num'
sample `proportion', count
tempfile file`i'
save `file`i''
restore
}
* Append
clear
forvalue i = 1(1)104 {
append using `file`i''
erase `file`i''
}
It would be great if I could get some help with what I am doing wrong in the loop. Thanks a lot!
I am facing issues with running a loop to draw my sample by a certain proportion. What I wish to achieve is, for instance, in strata 8 draw 1 consumer randomly from the larger pool of consumers in strata 8. The code for the same is as below but I keep running into a syntax error for the loop.
local numlist 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
local strata 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 22 23 24 25 27 30 31 32 33 35 36 37 38 39 41 42 43 44 45 46 47 48 49 50 51 54 55 56 57 58 59 61 62 63 65 66 67 68 70 71 72 73 74 76 77 78 80 81 82 83 84 86 88 89 90 93 94 95 96 98 100 102 103 104 105 106 107 108 109 110 111 112 113 114 116 117 119 121 122 123 124 127 128 129
local proportion 1 1 2 2 9 26 1 1 2 5 8 36 13 10 9 19 1 2 18 22 2 5 8 5 2 1 22 1 1 1 14 7 4 6 2 22 7 8 5 7 13 2 18 2 36 12 1 8 2 4 4 10 1 4 3 33 2 2 2 1 1 1 1 1 1 1 1 2 4 10 7 2 8 2 22 3 36 1 2 2 1 1 2 8 24 10 2 1 1 9 4 3 2 5 2 3 1 2 1 2 3 1 2 1
set seed 12345
local seed_num = 12345
forvalue i=1/104 of local numlist {
local strata : word `i' of `strata'
local proportion: word `i' of `proportion'
preserve
keep if strata==`strata'
set seed `seed_num'
sample `proportion', count
tempfile file`i'
save `file`i''
restore
}
* Append
clear
forvalue i = 1(1)104 {
append using `file`i''
erase `file`i''
}
It would be great if I could get some help with what I am doing wrong in the loop. Thanks a lot!

Comment