Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Loop not working to draw samples by strata

    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!

  • #2
    Code:
    forvalue i=1/104 of local numlist {
    should be

    Code:
    forvalue i = 1/104 { 

    as of local numlist is syntax that only works with foreach.

    Your code will never get to strata 105/129. That is not illegal but I guess that it is not what you want.

    Comment


    • #3
      Here is a revision of the code in post #1
      Code:
      local strata_l  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_l 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
      
      // see: help set_seed##frequency
      // do not repeatedly set the seed
      
      set seed 12345
      
      preserve
      clear
      save all104, replace emptyok
      restore
      
      forvalues i=1/104 {
      local strata : word `i' of `strata_l'
      local proportion : word `i' of `proportion_l'
      
      preserve
      keep if strata==`strata'
      sample `proportion', count
      append using all104
      save all104, replace
      restore
      
      }
      The code will reach strata 105 through 129, because a careful reading of the definition of strata_l above shows that there are some jumps in the series of strata - between 20 and 22, for example.
      Last edited by William Lisowski; 03 Feb 2023, 09:50.

      Comment


      • #4
        Hi Nick! The number list is just to make sure that in each loop only the strata and the corresponding proportion for the sample get picked up. I did make the change that you suggested but the problem still persists of invalid syntax.

        Comment


        • #5
          Thanks to William Lisowski for the correction on strata. My mistake.

          Comment


          • #6
            Thanks a lot Nick Cox and William Lisowski. This was really helpful!

            Comment

            Working...
            X