Announcement

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

  • 'Foreach' loop not working

    Hello Stata Experts,

    I wish to do a looping to generate Erreygers index by region but for some reason I am not getting any output. My outcome 'x' is 0 in 3 regions (v024==3; v024==5; v024==11). So I used a macro as suggested by Nick Cox in another post. Now, the 'foreach' loop is working but I get no output. I thought maybe it is the 'conindex' command but even a simple tab command is also not giving any output. I have pasted the example dataset and my code for your reference. I will request your guidance on why this is happening and how to get my looping to work.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int v021 byte(v023 v024) long v191 float wt int x
      5 24 11  186851  .067744 0
      6 15  6  -69810  .166429 0
      6 15  6  -37290  .166429 0
      6 15  6  -66434  .166429 0
     20 18  7  -21120 1.688765 1
     32 17  7  -47806 2.112978 1
     43 24 11   86568  .195201 0
     45  2  1  -36766  .604337 0
     45  2  1   -4259  .604337 1
     51  4  2 -114230  .190131 0
     52  6  3  -43784 3.328477 0
     68 22  9  -58055  .041074 0
     76 18  7  -56558 1.605871 0
     78  2  1  -74915  .645477 0
     78  2  1  -57221  .645477 0
     79  2  1  -24725  .512367 1
     81  1  1   41806  .754952 0
     90 23 10  111519  .382735 0
     97  1  1  127704  .607506 1
    100 23 10  162408  .527982 0
    105 20  8  -87175   .02789 0
    122 10  4  -62776 2.899588 0
    124 15  6  -69605  .098954 0
    124 15  6  -57613  .098954 1
    126 18  7  -28866 2.004016 0
    127  4  2 -100780  .212444 0
    127  4  2  -90617  .212444 0
    130  4  2  -80223  .126382 0
    131 11  4  -32123 3.079487 0
    133 21  9   84441  .058698 0
    153 23 10  171571  .459105 1
    156  2  1  -41646  .559356 0
    160  2  1  -56248  .672884 0
    161 15  6  -64178  .149906 0
    161 15  6  -35932  .149906 0
    165 15  6  -38586  .137104 0
    181  1  1  135218  .787829 0
    183 15  6   -8698  .077386 0
    185 24 11  -10975  .075809 0
    189  4  2 -105894  .029111 1
    189  4  2 -113238  .029111 1
    191  4  2  -85780  .053396 0
    192  4  2  -69047  .157307 0
    202 24 11   48198  .059896 0
    209 15  6  -74970   .16361 1
    219 20  8  -78017  .048316 0
    221 19  8  100730  .077621 0
    231 19  8   72833  .057112 0
    235  4  2 -109734  .043106 0
    235  4  2  -87428  .043106 0
    235  4  2  -33943  .043106 0
    236 23 10  141634  .653583 1
    242 25 11  -54747  .095418 0
    242 25 11  -71992  .095418 0
    245 10  4  -56100 3.043524 0
    257 21  9   59783  .054279 0
    257 21  9  -10269  .054279 0
    265 20  8  -61740   .05905 0
    267  6  3  -74145 2.215002 0
    268  1  1  106436  .839952 0
    268  1  1  204685  .839952 1
    276  4  2 -132448  .035753 0
    279  6  3  -53269 1.761423 0
    281 25 11  -61027  .101729 0
    282 24 11  105280   .20162 0
    289 11  4  -67786 3.640357 0
    296  2  1  -47547 1.948019 0
    300  2  1  -67123  .621639 0
    312  6  3  -28418 2.106929 0
    313 17  7  -69298 1.674679 0
    320 15  6  -51910  .154743 0
    324 15  6  -44296  .142338 1
    325 18  7  -23305 1.487217 0
    326 20  8  -67388  .040982 0
    336  4  2 -104019  .076858 0
    340  2  1  -53775 1.007922 0
    342 18  7  -47346 1.221068 0
    351  2  1  -56547  .664665 0
    355  2  1  -74216  .693804 0
    355  2  1  -77302  .693804 1
    357 22  9  -10946  .035653 0
    360 17  7  -40920  1.73668 0
    362  4  2  -82812  .193953 0
    367 13  5  -70102  .359625 0
    370 20  8  -36822  .012409 1
    372 10  4  -37574  3.12451 0
    374 15  6  -58953  .141171 0
    377 12  5    9731  .194199 0
    377 12  5  -84531  .194199 0
    381 22  9     941  .024807 0
    393 21  9    7986  .051921 1
    394 13  5  -80286  .200517 0
    396 21  9  150783  .027248 0
    406 18  7  -49365 1.939714 0
    416 15  6  -46350  .136107 1
    417 20  8  -65377  .032393 0
    417 20  8  -65377  .032393 0
    419 22  9  -53267  .038562 0
    423 10  4  -22726 2.884223 1
    425  6  3  -79502 2.315583 0
    end
    label values v024 V024
    label def V024 1 "tigray", modify
    label def V024 2 "afar", modify
    label def V024 3 "amhara", modify
    label def V024 4 "oromia", modify
    label def V024 5 "somali", modify
    label def V024 6 "benishangul", modify
    label def V024 7 "snnpr", modify
    label def V024 8 "gambela", modify
    label def V024 9 "harari", modify
    label def V024 10 "addis adaba", modify
    label def V024 11 "dire dawa", modify
    label values x B
    label def B 0 "No", modify
    label def B 1 "Yes", modify
    Code:
    *Survey setting
    svyset v021 [pweight=wt], strata(v023) vce(linearized) singleunit(centered) 
    
    *Check frequency
    tab v024 x
    
    *Looping
    local x 1 2 4 6 7 8 9 10
    foreach x of local x{
    conindex ifa if v024==`x', rankvar(v191) erreygers bounded limits(0 1) svy 
        gen ciw`x'=r(CI)
    }
    Thank you
    Deepali
    Deepali Godha

  • #2
    I have three comments here.

    0. I am surprised to hear that your loop is working, as it seems illegal to me.

    1. You can't use here the same name for a macro holding a set and a macro holding each individual in the set.

    You could do this

    Code:
    local X 1 2 4 6 7 8 9 10
    foreach x of local X {
        conindex ifa if v024==`x', rankvar(v191) erreygers bounded limits(0 1) svy
        gen ciw`x'=r(CI)
    }
    or you could do this


    Code:
    foreach x in 1 2 4 6 7 8 9 10  {
        conindex ifa if v024==`x', rankvar(v191) erreygers bounded limits(0 1) svy
        gen ciw`x'=r(CI)
    }
    With your code you have a loop over one item, the entire set, which is not itself illegal but the implication is that you are asking inside the loop


    Code:
    conindex ifa if v024==1 2 4 6 7 8 9 10,, rankvar(v191) erreygers bounded limits(0 1) svy
    which is illegal as well as not what you want.


    2. You are creating 8 new variables, each holding a constant. I think you would be better off with this

    Code:
    gen ciw = .
    
    foreach x in 1 2 4 6 7 8 9 10  {
        conindex ifa if v024==`x', rankvar(v191) erreygers bounded limits(0 1) svy
        replace ciw =r(CI) if v024==`x'
    }
    Last edited by Nick Cox; 27 Feb 2023, 00:45.

    Comment


    • #3
      Many thanks Nick, this worked. And thanks for explaining.
      Deepali Godha

      Comment


      • #4
        If anyone is puzzled by

        Code:
         
         v024==1 2 4 6 7 8 9 10,,
        you're right: the second comma was a typo of mine and is a distraction from the main error.

        Comment

        Working...
        X