Announcement

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

  • Nested loops

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte nbcas float(nbcas_cat nbcas_b nbcas2p) double(P19_POP1564 P19_H1564 P19_CHOM1564) float(P19_TCHOM1564 chom)
    0 0 0 0 1584  752 108  7 0
    1 1 1 0 1207  604  65  5 0
    2 2 1 1 1848  899  99  5 0
    0 0 0 0 1906  888 143  8 0
    0 0 0 0 1276  632 110  . 0
    0 0 0 0   14    8   0  0 .
    0 0 0 0   70   70   0  0 .
    1 1 1 0 1068  482  59  6 0
    0 0 0 0 1672  776 120  7 0
    0 0 0 0 1578  745  86  5 0
    0 0 0 0 1426  630 104  7 0
    1 1 1 0 2047  963 140  7 0
    0 0 0 0 1441  683 128  . 0
    0 0 0 0 1540  748 103  7 0
    1 1 1 0 1123  504  63  6 0
    0 0 0 0    0    0   0  . .
    0 0 0 0    0    0   0  . .
    0 0 0 0 1863  872 102  5 0
    end
    Hello everyone,

    I'm using Stata/SE 17.0 for Mac, here's a sample of my data.
    thanks for kind help, i'm failing to solve my problem

    I'm trying to make a nested loop

    I want to make a chi square with all combinations

    Code:
    tab X Y, row chi2
    X : nbcas nbcas_cat nbcas_b nbcas2p
    Y : chom nodip surocc men2 logvac pacti loc hlm fmonop pseule imm deci cath prop etr hmen pauv rev psoc

    I managed to make the code work with only one loop :

    Code:
    ds chom nodip surocc men2 logvac pacti loc hlm fmonop pseule imm deci cath prop etr hmen pauv rev psoc
        foreach v of varlist `r(varlist)' { 
            tab nbcas `v', row chi2 
            }
    but i was trying to figure out if there's a way to make everything work.

    ideally i would also like all the p-values to be displayed so that i can copy and paste them in my results at once.

    Thank you so much !!

    Sybille

  • #2
    Code:
    foreach x of varlist nbcas nbcas_cat nbcas_b nbcas2p {
        foreach y of varlist chom nodip surocc men2 logvac pacti loc hlm fmonop pseule imm deci cath prop etr hmen pauv rev psoc {
            tab `x' `y', row chi2
        }
    }

    Comment


    • #3
      There are better ways to approach this (frames, postfile, etc.) but on the assumption that you have at least 76 observations in the dataset, here is some code, completely untested, for a quick and dirty approach.

      Code:
      local X  nbcas nbcas_cat nbcas_b nbcas2p
      local Y chom nodip surocc men2 logvac pacti loc hlm fmonop pseule imm deci cath prop etr hmen pauv rev psoc
      
      gen Xname = "" 
      gen Yname = "" 
      gen double chi2 = . 
      gen double pval = . 
      gen rows = . 
      gen cols = . 
      
      local i = 1 
      
      foreach x of local X {
           foreach y of local Y { 
                 tab `x' `y', row chi2 
                 quietly { 
                       replace Xname = "`x'" in `i' 
                       replace Yname = "`y'" in `i' 
                       replace chi2 = r(chi2) in `i' 
                       replace pval = r(p) in `i' 
                       replace rows = r(r) in `i' 
                       replace cols = r(c) in `i' 
                }
                local ++i 
           }
      } 
      
      list Xname-cols



      Comment

      Working...
      X