Announcement

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

  • foreach/loops with several varlists/macros

    Hi again everyone,

    I would appreciate feedback on what to do if I wanted to use different local macros in a loop, as follows
    Code:
     foreach in local macro1 macro2
    It might have to use instead
    Code:
    foreach of varlist1 varlist2
    but even doing that, I'm afraid one cannot use different varlist in an argument (if so, I'd appreciate comments on how to generate different varlists and how to include them in a loop)

    Specifically, I want to achieve the following:

    Code:
    #delimit;
    local combo1 one var2;
    local combo2 one var2 four X; //2 vars > `combo1'
    local combo3 one var2 four X var5 six v7; //5 vars > `combo2'
    
    foreach combinations of local combo1 combo 2 combo3 {;
    
    foreach orderedDV in ord4_DV ord_DV {;
    use fictional.dta.dta, clear; //.dta below
    qui oprobit `orderedDV' `combinations' eight v9 x10 X11;
    qui outreg,
     `merge';
     local merge "merge";
    };       
    };
    Thanks for your support
    (a less elaborated version of this question posted here: https://www.statalist.org/forums/for...-foreach/page2

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double one float(var2 four X) double var5 float six double(v7 eight) float(v9 x10 X11 ord4_DV ord_DV)
     1.625886252561462 0 0  .3428978   80.2277913977242 1039.6466 .15144800853173346             944285  95.64283  1         0 0 0
    1.8062101772211463 0 0  .3428978  80.41157452097417 1032.9587   .179423490405604   968511.200000003   95.3998  4  .6931472 0 0
    1.9865341018807499 0 0  .3428978  80.59535764422412 1031.6782  .2073989722794778  992737.3999999985  95.15678  9 1.0986123 0 0
    2.1668580265404103 0 0  .3428978  80.77914076747402 1028.1687 .23537445415335156 1016963.6000000015  94.91376 16 1.3862944 0 0
     6.428323604839978 0 0 .29107344  91.58278613489686 1157.4456  1.874065391629191            2487367  81.41196  1         0 0 0
     6.976726304116937 0 0 .29107344  91.86232540255321 1083.7776 1.7830958918745239  2558787.399999976  80.59206  4  .6931472 0 0
     7.525129003393886 0 0  .3168855  92.14186467020966 1029.4856 1.6921263921198602  2630207.799999982  79.77216  9 1.0986123 0 0
     8.073531702670834 0 0  .3168855  92.42140393786599  1217.595 1.6011568923651964  2701628.199999988  78.95226 16 1.3862944 0 0
     3.569230333859226 0 1  .3622786  81.29756320733137 1011.9991 1.4304610985663322             424041  89.02796  1         0 0 0
     3.951036959859266 0 1  .3622786  81.99011726246977 1049.1273 1.4677014602501544   441666.799999997  88.39745  4  .6931472 0 0
     4.332843585859337 0 1  .3622786  82.68267131760808  1038.007 1.5049418219339827  459292.6000000015  87.76692  9 1.0986123 0 0
    4.7146502118592934 0 1  .3622786   83.3752253727464  1329.303 1.5421821836178111  476918.3999999985  87.13641 16 1.3862944 0 0
     10.12148738433651 1 0 .27410138  70.98838985418908  1372.471 15.454945533122686             690689  71.28032  1         0 0 0
    10.580860615259894 1 0 .27410138   71.5948670116752 1354.9578 15.012172424179425   703497.200000003  70.56196  4  .6931472 0 0
    11.040233846183355 1 0 .27410138  72.20134416916153  1462.763 14.569399315236183  716305.4000000022   69.8436  9 1.0986123 0 0
    11.499607077106816 1 0 .27410138  72.80782132664763  6507.198  14.12662620629294  729113.6000000015  69.12524 16 1.3862944 0 0
     5.403186151857863 1 0  .4445573  89.41211538377856 1078.2158 .15024382412156667            2298070  86.41884  1         0 0 0
     5.799135338451379 1 0  .4445573   89.5378439581931  1053.952 .17319747080717462            2337496  85.94479  4  .6931472 0 0
    6.1950845250448765 1 0  .4445573  89.66357253260762 1113.9613 .19615111749278924            2376922  85.47073  9 1.0986123 0 0
     6.591033711638374 1 0  .4445573  89.78930110702214 1369.5093 .21910476417839675            2416348  84.99668 16 1.3862944 0 0
     2.429108761635525 1 0 .23452406  85.59065435372733  799.2452  .6404865250984649             542627  93.04836  1         0 0 0
    2.7082598172072494 1 0 .23452406   85.9927172871636  776.4243  .6286909927850921  547700.8000000008  92.64496  4  .6931472 0 0
      2.98741087277881 1 0 .23452406  86.39478022059984  809.6693  .6168954604717207  552774.5999999996  92.24156  9 1.0986123 0 0
     3.266561928350484 1 0 .23452406  86.79684315403608 1074.7703  .6050999281583493  557848.4000000004  91.83817 16 1.3862944 0 0
    11.328315087485196 1 0  .3096812 45.700264123571884  329.6054 24.615418294459378            3920892  63.82681  1         0 0 0
    12.049506653510889 0 0  .2877287   46.1084426899107  327.7537 24.897666150510872  3995405.399999976  63.39743  4  .6931472 0 0
     12.77069821953637 0 0  .2877287  46.51662125624955  340.1454   25.1799140065624  4069918.799999982  62.96806  9 1.0986123 0 0
     13.49188978556208 0 0  .2877287   46.9247998225884 453.13205 25.462161862613925  4144432.199999988  62.53868 16 1.3862944 0 0
     5.784126408043219 1 0 .28922045  82.52616276879708 1191.5645 3.2080950547813347            3052907  84.64978  1         0 0 0
     6.127745844480273 1 0 .28922045  82.91992954990201 1138.8712   3.24478480681222  3090614.400000006  84.09225  4  .6931472 0 0
     6.471365280917212 1 0 .28922045   83.3136963310069 1112.8174  3.281474558843101  3128321.799999997  83.53472  9 1.0986123 0 0
     6.814984717354264 1 0 .28922045   83.7074631121119  914.0846  3.318164310873996  3166029.200000003   82.9772 16 1.3862944 0 0
    3.1352179759330334 0 1  .3529178  99.76386478051336 2075.0325 1.8312791157032153            8605239  90.45133  1         0 0 0
    3.3476094002289756 0 1 .28028113  99.74145235075906 2059.3784 1.7688706340587004  8628374.400000006  89.84621  4  .6931472 1 1
     3.560000824524991 0 1 .28028113  99.71903992100476  2200.287 1.7064621524141756  8651509.800000005  89.24107  .         . 0 0
    3.7723922488210064 0 1 .28028113  99.69662749125047  2091.176  1.644053670769651  8674645.200000003  88.63595  .         . 0 0
     2.998769208255071 1 0 .24179547  63.78683487717278  658.3974 1.9726094155826175            1448661  90.37295  1         0 0 0
     3.266931167139319 1 0 .24179547  64.47680167373869  688.4044 1.9964237608771924  1460752.200000003  90.08989  4  .6931472 0 0
    3.5350931260236393 1 0 .24179547  65.16676847030476  701.4589 2.0202381061717674 1472843.4000000022  89.80683  9 1.0986123 0 0
     3.803255084907846 1 0 .24179547   65.8567352668706  874.9973 2.0440524514663423 1484934.6000000015  89.52377 16 1.3862944 0 0
    1.0809276024698093 0 0  .4039429  67.20483582355857  583.2146 .26392918431091744            4663032   96.4067  1         0 0 0
    1.2256758818023172 0 0  .3887023  67.70373802857341 576.77344 .25919301037665754            4709188  96.19558  4  .6931472 0 0
    1.3704241611348493 0 0  .3887023  68.20264023358811  607.3845  .2544568364423956            4755344  95.98447  9 1.0986123 0 0
    1.5151724404673814 0 0  .3887023  68.70154243860293  822.7074  .2497206625081354            4801500  95.77335 16 1.3862944 0 0
     3.700096991572741 1 0 .25315502   55.3051013281059  442.8051  13.87345755994032            3079649  89.17783  1         0 0 0
     3.947205371906307 1 0 .25315502  55.75508420128597 458.49365 13.916879345915888 3086759.6000000015   88.9549  4  .6931472 0 0
      4.19431375223985 1 0 .25315502 56.205067074466115  464.0776 13.960301131891441  3093870.200000001  88.73198  9 1.0986123 0 0
     4.441422132573393 1 0 .25315502  56.65504994764615   516.378 14.003722917867009 3100980.8000000007  88.50906 16 1.3862944 0 0
     4.393916418521993 1 0  .3744059  49.32449629650504  462.5535  17.21740170053415            2235591  90.77811  1         0 0 0
    4.6092670710933135 1 0  .3744059   49.9226085562143  457.9097 16.852542705557198  2257575.599999994  90.46156  4  .6931472 0 0
      4.82461772366463 1 0  .3744059 50.520720815923596 465.99945   16.4876837105802 2279560.1999999955  90.14501  9 1.0986123 0 0
     5.039968376235947 1 0  .3744059  51.11883307563289  664.6369 16.122824715603087  2301544.799999997  89.82845 16 1.3862944 0 0
    1.6604233911979147 0 0  .3425416  84.55077995862703  808.6878  .7084569465197023            6322002  95.38914  1         0 0 0
    1.8052079930340597 0 0  .3425416  84.86961287748409  816.2131  .7111236437658821  6408024.199999988  95.15725  4  .6931472 0 0
    1.9499925948702526 0 0  .2421959  85.18844579634117  809.7563  .7137903410120625  6494046.400000006  94.92536  9 1.0986123 0 0
    2.0947771967063886 0 0  .2421959  85.50727871519825   981.598   .716457038258242  6580068.599999994  94.69347 16 1.3862944 0 0
    3.1720620010283516 1 0 .23922597  86.31504183577434  611.4417 3.2617389332892155           13096686  91.21169  1         0 0 0
    3.4099583710464003 1 0 .23922597   86.4715849301763  600.3093  3.129293038805997 13278847.799999952  90.73677  4  .6931472 0 0
    3.6478547410644637 1 0 .23922597  86.62812802457827 587.05206 2.9968471443227713 13461009.599999964  90.26186  9 1.0986123 0 0
     3.885751111082527 1 0 .23922597  86.78467111898024  666.5426 2.8644012498395455 13643171.399999976  89.78695 16 1.3862944 0 0
    1.5540937062730027 1 0   .234555  65.40350711687654  443.0032 3.5020551211042728            3985667  94.76058  1         0 0 0
    1.7064224679576228 1 0   .234555  65.91076032772003  441.1185  3.449094623838775 3981748.1999999993  94.54282  4  .6931472 0 0
    1.8587512296422801 1 0   .234555   66.4180135385634   437.026  3.396134126573287 3977829.3999999994  94.32507  9 1.0986123 0 0
    2.0110799913268806 0 1  .2351979  66.92526674940689  593.4764  3.343173629307799 3973910.5999999996   94.1073 16 1.3862944 0 0
     6.516315865275805 1 0  .5515163  85.43209781289221  680.6651  2.314494356097722            1555296  83.60526  1         0 0 0
     6.832759825653625 0 0 .41662335  85.55744414514041  725.7344  2.205462045581669 1566816.6000000015  83.16281  4  .6931472 0 0
     7.149203786031421 0 0 .41662335  85.68279047738861  704.7587  2.096429735065584 1578337.1999999993  82.72036  9 1.0986123 0 0
     7.465647746409218 0 0 .41662335  85.80813680963684  833.1968 1.9873974245495276 1589857.8000000007   82.2779 16 1.3862944 0 0
     2.533186261458294 0 0  .2995501  64.16405396740873  456.2836 4.5636806772783745             920185  91.82056  1         0 0 0
    2.7638124858579545 0 0  .2995501  64.61955802391924  497.1729  4.641607986201393  926084.8000000008  91.53119  4  .6931472 0 0
    2.9944387102576684 0 0  .2995501  65.07506208042969  500.9337  4.719535295124416  931984.5999999996  91.24183  9 1.0986123 0 0
    3.2250649346573255 0 0  .2995501  65.53056613694014  678.8785  4.797462604047439  937884.4000000004  90.95246 16 1.3862944 0 0
     5.196496425144511 0 0 .27559844  93.40738903446692   1465.09 .45536220988937287            3834141  87.92954  1         0 0 0
     5.468058393436422 0 0 .27559844  93.59793536878595 1425.0013  .5230800416981367  3907171.199999988  87.49634  4  .6931472 0 0
    5.7396203617283845 0 0 .27559844  93.78848170310494  1470.062  .5907978735068866  3980201.400000006  87.06314  9 1.0986123 0 0
     6.011182330020347 0 0 .27559844  93.97902803742392 1626.6957  .6585157053156081  4053231.599999994  86.62994 16 1.3862944 0 0
     6.843009045398567 1 0    .30087 44.532673794225545  341.9523  37.10744548960403            3438765  84.84643  1         0 0 0
    7.2130926928348345 1 0    .30087  45.04090528663767  355.7958  36.71952348947059  3452376.200000003  84.49333  4  .6931472 0 0
    7.5831763402709385 1 0    .30087  45.54913677904972  366.7091  36.33160148933712 3465987.4000000022  84.14024  9 1.0986123 0 0
     7.953259987707156 1 0    .30087  46.05736827146177 473.14465 35.943679489203646 3479598.6000000015  83.78715 16 1.3862944 0 0
     3.587872088208725 1 0  .3972125   68.2829507280931 587.58386 13.038086283782633            5076686  91.60835  1         0 0 0
    3.7951483390507406 1 0  .3972125  68.74716346808032 584.64417 12.770981636238503  5137975.400000006  91.36501  4  .6931472 0 0
     4.002424589892769 1 0  .3972125  69.21137620806746  575.2612 12.503876988694401  5199264.799999997  91.12167  9 1.0986123 0 0
     4.209700840734797 1 0  .3972125  69.67558894805472  621.0908   12.2367723411503  5260554.200000003  90.87833 16 1.3862944 0 0
    1.6744213867917677 0 0  .2512303   67.5687492612009  978.5259 2.0643123696989103            1404306  95.27264  1         0 0 0
    1.8277124024828026 0 0  .2512303  68.02996654260801  951.8215 1.9873254537426988  1443072.599999994  94.99509  4  .6931472 0 0
    1.9810034181738274 0 0  .2512303  68.49118382401514   953.666 1.9103385377864868  1481839.200000003  94.71754  9 1.0986123 0 0
     2.134294433864852 0 0  .2512303  68.95240110542227  1110.721 1.8333516218302748  1520605.799999997  94.43999 16 1.3862944 0 0
     9.103127789403667 1 0 .25404277  82.46497280456431  1269.206 22.978865352998643             874963  73.16843  1         0 0 0
     9.592762806820815 1 0 .25404277  83.08842847878555  1324.626  22.22111946343034  927032.1999999881  72.31599  4  .6931472 0 0
       10.082397824238 1 0 .25404277  83.71188415300685 1284.5726   21.4633735738621  979101.3999999911 71.463554  9 1.0986123 0 0
    10.572032841655073 1 0 .25404277  84.33533982722838 1391.7313  20.70562768429363  1031170.599999994  70.61112 16 1.3862944 0 0
     3.697594113144527 1 0 .27333385  59.04386437965346  593.7335 11.700991624633991            2299360  91.95584  1         0 0 0
    3.8747734583964757 1 0 .27333385  59.76376914996945  568.2257 11.562171885918076  2321570.799999997  91.73528  4  .6931472 0 0
     4.051952803648419 1 0 .27333385 60.483673920285355 578.41705 11.423352147202138 2343781.6000000015  91.51472  9 1.0986123 0 0
     4.229132148900362 1 0 .27333385  61.20357869060149  751.7444   11.2845324084862 2365992.3999999985  91.29416 16 1.3862944 0 0
      2.69539632708988 1 0  .3036564  67.42243512017294  605.8975 2.2194282063340083            2536844  86.83486  1         0 0 0
    2.9488462155136403 1 0  .3036564  68.09763422295259  591.4226  2.042290619126163 2551163.6000000015  86.58202  4  .6931472 0 0
    3.2022961039374422 1 0  .3036564  68.77283332573234   610.177 1.8651530319182825 2565483.1999999993  86.32919  9 1.0986123 0 0
     3.455745992361244 1 0  .3036564   69.4480324285123  817.7052 1.6880154447104587 2579802.8000000007  86.07635 16 1.3862944 0 0
     4.300781833214628 1 0 .25157052  83.09168959962905  956.7456  2.846443632044493            2216969  87.85005  1         0 0 0
     4.633274636341866 1 0 .25157052  83.63075663974064  948.7375  2.769551712642169  2252547.400000006  87.37349  4  .6931472 0 0
    4.9657674394691185 1 0 .25157052  84.16982367985224  910.7047 2.6926597932398124  2288125.799999997  86.89693  9 1.0986123 0 0
    5.2982602425963705 1 0 .25157052  84.70889071996362 1088.4086  2.615767873837484  2323704.200000003  86.42037 16 1.3862944 0 0
    11.138533133808606 1 0   .372205 53.735142023935566  507.0536  3.726764425613116            1891829  70.44538  1         0 0 0
     11.78486686565202 1 0   .372205 53.986264846629865 519.45386   3.57327367733086            1911457  69.96033  4  .6931472 0 0
    12.431200597495263 1 0 .27756268  54.23738766932422  519.9532 3.4197829290486084            1931085  69.47527  9 1.0986123 0 0
    13.077534329338505 1 0 .27756268  54.48851049201852  1028.559 3.2662921807663565            1950713  68.99021 16 1.3862944 0 0
     7.142685914902612 1 0  .4077575  85.42460433630124  892.0453  .7052254162943408            2753222  82.89744  1         0 0 0
     7.597347070327828 1 0  .4077575  85.79144487141741   866.207  .7170912437707706  2807425.199999988   82.1617  4  .6931472 0 0
     8.052008225753184 1 0  .4077575  86.15828540653365  893.6956  .7289570712471978  2861628.399999991  81.42596  9 1.0986123 0 0
      8.50666938117854 1 0  .4077575  86.52512594164978  1106.382  .7408228987236285  2915831.599999994  80.69022 16 1.3862944 0 0
    2.4009864477700007 0 1 .26480663  78.45698210972674  438.3435  3.148273007768543             962646  93.43553  1         0 0 0
      2.56402096583264 0 1 .26480663  78.41004411203811  451.3303  3.024218188743447   983758.200000003  93.21018  4  .6931472 0 0
     2.727055483895356 0 1 .26480663  78.36310611434949  438.8249  2.900163369718342 1004870.3999999985  92.98484  9 1.0986123 0 0
     2.890090001958015 0 1 .26480663  78.31616811666086 571.98645  2.776108550693209 1025982.6000000015   92.7595 16 1.3862944 0 0
     6.951624517384995 1 0  .3668758 59.053159115498325  456.7104 10.352416139107056            6908975  82.86982  1         0 0 0
     7.264214845439824 1 0  .3668758  59.36807513104327 462.63785 10.181692857174994  6949222.800000012  82.56607  4  .6931472 0 0
     7.576805173494677 1 0  .3668758 59.682991146588165  474.7071 10.010969575242882  6989470.600000009  82.26233  9 1.0986123 0 0
     7.889395501549529 1 0  .3668758  59.99790716213295  667.8167  9.840246293310827  7029718.400000006  81.95858 16 1.3862944 0 0
     5.700725481091056 1 0 .27599582  81.33788844597488  664.5217  37.31502298865404            1658210   84.2753  1         0 0 0
     6.008429670998453 1 0 .27599582  81.66559550378747   682.808  36.51028731891893  1690357.599999994  83.86362  4  .6931472 0 0
     6.316133860905779 0 0   .254479   81.9933025616001  667.4993 35.705551649184144 1722505.1999999955  83.45194  9 1.0986123 0 0
     6.623838050813106 0 0   .254479  82.32100961941274  817.7228 34.900815979449135  1754652.799999997  83.04027 16 1.3862944 0 0
    1.6320801412519115 0 1 .23958826 53.343577544492135   421.662 .15453545145887523 1356426.4000000004  95.13327  1         0 0 0
     1.759842349872514 0 1 .23958826 54.124252687875014  434.8265 .18874250053178798 1359242.7999999998   95.0572  4  .6931472 0 0
    1.8876045584930807 0 1 .23958826 54.904927831257964  441.6638  .2229495496047207 1362059.2000000002  94.98112  9 1.0986123 0 0
     2.015366767113676 0 1 .23958826 55.685602974640915  567.3516  .2571565986776534 1364875.6000000006  94.90505 16 1.3862944 0 0
    end
    ------------------ copy up to and including the previous line ------------------
    Victor Cruz

  • #2
    This is hard to follow because so much is fantasy syntax and yet it is not at all clear what you want to do. I think those are two key reasons why your previous post got no replies.

    On syntax: the legal forms for foreach are documented at help foreach and there are no others to be found by free association. Thus of the loops you give only one looks both legal and likely to work as you wish.

    Code:
    foreach orderedDV in ord4_DV ord_DV {
     ...  
    }
    A single varlist can be formed by combining varlists and a single local macro can be formed by combining the contents of various local macros. So, that is the syntactical way to get around your imagined problem.

    Beyond that,

    1. I don't understand whether your three variable lists are to be applied separately, as your attempted syntax seems to imply, or added list by list to the predictors. In either case, check out nestreg which may be what you should use.

    2. You're using the same dataset repeatedly. So, there is no obvious need to read it in repeatedly

    3. outreg is a user-written command, as you are asked to explain (FAQ Advice #12).

    4. Your two ordered response variables are identically zero in the data example, so no useful models can be fitted to the example.


    EDIT:

    As above I am unclear what you want to do, but this shows some technique:


    Code:
    use fictional.dta.dta, clear
    
    local combo1 one var2
    local combo2 one var2 four X
    local combo3 one var2 four X var5 six v7 
    
    foreach orderedDV in ord4_DV ord_DV {
        forval j = 1/3 { 
            oprobit `orderedDV' `combo`j'' eight v9 x10 X11
        }       
    }
    Last edited by Nick Cox; 11 Jul 2017, 04:46.

    Comment


    • #3
      Thanks a lot for the detailed feedback, Prof. Nick Cox.

      As above I am unclear what you want to do, but this shows some technique:
      My goal (achieved with the loop that you suggested) was to make Stata estimate (in a loop, to save space in my .do) 3 ordered probit models that differed in their varlist, for which I thought I could first create a macro, and then add them to the loop, as can be seen in lines 2-5 of my post. Your suggestion solved my issue, I appreciate the quick response.

      This is hard to follow because so much is fantasy syntax and yet it is not at all clear what you want to do. I think those are two key reasons why your previous post got no replies.
      I agree, thanks for the remark
      1. I don't understand whether your three variable lists are to be applied separately, as your attempted syntax seems to imply, or added list by list to the predictors. In either case, check out nestreg which may be what you should use.
      thanks for the suggestion, I was not aware of it.
      2. You're using the same dataset repeatedly. So, there is no obvious need to read it in repeatedly
      I completely agree
      3. outreg is a user-written command, as you are asked to explain (FAQ Advice #12).
      thanks, I will keep that in mind in future posts
      4. Your two ordered response variables are identically zero in the data example, so no useful models can be fitted to the example.
      You are right. When I extracted one small portion of my real .dta, I failed to include obs. whose Y actually varied; I just kept the first 5 years of each subject. It was intended as a fictional illustration. I will use Stata's example .dtas in the future.
      Victor Cruz

      Comment

      Working...
      X