Hello.

I have data with the following fields:

Estimates for females by age band
wf0to4_wtd_pop
wf5to14_wtd_pop
wf15to44_wtd_pop
wf45to64_wtd_pop
wf65to74_wtd_pop
wf75to84_wtd_pop
wf85plus_wtd_pop

Multipliers for females:
f0to4inr
f5to14inr
f15to44inr
f45to64inr
f65to74inr
f75to84inr
f85plusinr

I have the same set if variables as above repeated, but with the 'f' replaced with an 'm' e.g. wm0to4_wtd_pop. These variables relate to males

I also have some constants held in local variables:

local wIMD1 = 0.00
local wIMD2 = 1
local wIMD3 = 2
local wIMD4 = 4
local wIMD5 = 5
local wIMD6 = 7
local wIMD7 = 9
local wIMD8 = 10
local wIMD9 = 12
local wIMD10 = 17

II need to calculate the following:

replace wf0to4_wtd_pop = wf0to4_wtd_pop + `wIMD1' * f0to4inr if IMD == 1

going from wIMD1 & IMD1 through to wIMD10 to IMD10, and when I have finished with population wf0to4_wtd_pop and multiplier f0to4inr, go on to the next age band and mulitplier : wf5to14_wtd_pop & f5to14inr. when I have done this for the female populations I need to do the same for the male populations( same variables as above, but with the f replaced with a m in the variable name.

I started to write a loop - but I have got stuck!

local wIMD1 = 0.00
local wIMD2 = 1.2792
local wIMD3 = 2.5312
local wIMD4 = 3.977
local wIMD5 = 5.1709
local wIMD6 = 5.5843
local wIMD7 = 7.7938
local wIMD8 = 9.3196
local wIMD9 = 10.3341
local wIMD10 = 13.7238

foreach var of varlist *_wtd_pop {

replace `var' = `var' + wIMD1 * f0to4inr if IMD == 1 }

However, this only works for IMD1, which I could run individually - but I would like to somehow specify the correct multiplier through the loop: f0to4inr. This variable should change to corresponding ageband, so for wf5to14_wtd_pop the calculation should use f5to14inr and so on.

Can anyone advise on how to set up a loop to do this please?


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input double(wf0to4_wtd_pop wf5to14_wtd_pop wf15to44_wtd_pop wm0to4_wtd_pop wm5to14_wtd_pop wm15to44_wtd_pop m0to4InclNewReg m5to14InclNewReg m15to44InclNewReg m45to64InclNewReg f0to4InclNewReg f5to14InclNewReg f15to44InclNewReg)
-253.38479999999998          -5475.3558          7421.8916            179.802  -5632.671600000001 -13726.774800000001  133  256  820  560  134  267  775
         -1206.5112         -21461.6342           29271.32           544.5432 -23562.175499999998         -56588.1984  486 1057 3312 2661  516 1031 3172
 -851.2416000000001         -13223.0624         21034.5784             513.72         -14130.1143 -41613.377400000005  434  637 2458 1409  408  638 2222
-405.89399999999995          -9201.9266 12094.874399999999            179.802  -9816.625800000002 -21294.917100000002  187  442 1259 1098  193  443 1284
           -1038.03         -18246.4774 26267.557999999997            488.034 -22594.898699999998  -47550.85110000001  468 1017 2799 2002  452  877 2824
 -582.4451999999999 -12421.858400000001         17265.4764 308.23199999999997         -13709.5893 -33403.525200000004  322  615 1968 1351  274  596 1857
 -562.1579999999999 -10121.648400000002           14783.98           220.8996         -12471.5025 -28153.314000000002  244  559 1652 1205  258  486 1595
 -763.4423999999999 -13493.456600000001 20015.394399999997           292.8204 -15323.065200000001          -36255.708  344  688 2128 1596  358  647 2156
 -375.3323999999999          -7540.7698 10088.677599999999            205.488  -9170.794800000001 -19423.223100000003  202  412 1147  709  177  367 1079
-475.90919999999994  -9171.827800000001         12411.6824           241.4484  -9751.309200000002         -21427.2324  238  440 1264  997  231  445 1315
           -215.874          -3890.1704          6999.7348           169.5276          -5100.5952         -12879.8532  127  232  772  571  111  188  725
 -775.5287999999999         -15293.6136         21600.6492           297.9576 -16211.817000000001         -48210.2001  347  730 2837 1199  365  738 2310
         -1137.2244         -27386.3954         34532.5484            488.034         -30662.2782         -63421.5915  534 1382 3731 2697  512 1319 3700
          -579.6432         -13112.2836 16189.154799999998           261.9972          -14722.002          -31138.641  279  660 1826 1511  249  630 1745
          -634.8072         -13295.3454          16738.424           323.6436         -14957.9505          -29695.545  322  671 1738 1118  292  639 1813
          -537.5928 -11466.538400000001 15441.057999999999           339.0552         -11333.5938 -26271.751500000002  315  510 1563  983  268  554 1625
           -996.372 -17886.215400000005         23927.0928           426.3876         -19390.3308         -42404.2128  410  872 2492 1843  447  861 2577
          -508.1148         -11711.2466 15783.624399999999            205.488 -12840.287400000001          -28581.912  238  578 1676 1416  225  563 1694
          -629.9316          -9723.9772         18061.0532           251.7228         -11445.1452          -41987.124  252  512 2500  841  276  466 1921
-256.71000000000004          -5558.6376          7815.6548 154.11599999999999          -6329.8746         -15031.8657  126  286  897  577  127  270  823
-1639.9403999999997 -29451.447200000002 37962.975999999995           791.1288         -33754.1904         -72420.3279  797 1516 4271 2598  753 1418 4052
         -1038.7212         -18436.8496 27201.283199999998           364.7412 -21081.600899999998          -47778.399  428  951 2834 2273  449  892 2880
          -441.2004  -8047.771000000001         16095.7064 308.23199999999997 -8107.0100999999995         -43079.2155  254  367 2607  792  233  391 1657
         -1137.2244         -19068.5924 27619.239599999997            565.092         -22415.8275         -48001.3794  551 1009 2838 1858  512  920 2925
           -810.798         -16866.7136         24485.2464            436.662 -18543.044700000002         -41840.2431  449  833 2467 1723  368  812 2625
          -651.0408 -12110.366600000001 18367.586799999997           339.0552         -13224.4839 -32364.170100000003  322  597 1913 1589  281  587 1952
-1430.7912000000001          -33770.112         41904.2828           724.3452 -38277.941399999996  -76277.01060000001  747 1722 4498 3058  702 1626 4474
 -693.1283999999999         -13687.8794 18438.467999999997           313.3692         -14515.4097         -40580.6877  320  651 2401 1336  299  659 1974
         -1151.2536 -23162.735600000004         30786.7332           518.8572         -26093.7594         -56371.0374  491 1174 3334 2339  518 1118 3282
          -437.1468          -8401.8092 15957.557999999999           200.3508          -9320.5098 -36721.907100000004  201  418 2207  833  206  404 1665
-314.76959999999997  -6231.840200000001          7517.1412           113.0184          -6992.5842 -13699.707300000002  158  314  805  475  150  299  815
 -663.5568000000001         -15775.6532 20881.224000000002            436.662 -17369.175600000002  -33692.71950000001  374  784 2003 1579  301  764 2196
 -574.9355999999999         -11136.3752         14442.1448             256.86         -12230.7849         -26617.7484  265  551 1564 1258  262  536 1555
          -609.6444          -9928.3122 15525.374399999999           303.0948          -12656.079 -29826.884700000002  302  570 1751 1338  260  477 1674
         -1124.5776 -19177.560200000004           26163.98           467.4852 -21739.173300000002          -50214.762  537  979 2960 1932  500  923 2812
 -645.9408000000001          -10817.573         15329.8104           292.8204         -12425.2677         -28326.5802  266  559 1674 1225  318  521 1632
         -1115.8536 -21818.207800000004          32188.336           518.8572         -24307.8165 -61998.768000000004  496 1091 3640 2688  523 1051 3473
-370.45680000000004 -6916.7660000000005         10743.0176           190.0764  -8991.723600000001 -19732.836600000002  177  404 1162  924  161  332 1162
 -730.8072000000001         -14336.4826 20265.799600000002           344.1924         -17501.2758         -38818.9557  386  786 2293 1650  351  691 2174
-256.14959999999996          -7931.4928  9909.256399999998           123.2928          -8635.4163         -17074.6155  131  389 1007 1066  122  382 1066
          -566.0436         -10189.8808         13138.1184           261.9972 -12333.896999999999 -22406.193000000003  285  554 1314  805  256  490 1419
-381.03000000000003          -8412.0836         11808.5088           133.5672          -9453.3462         -22239.9981  190  426 1313  911  182  404 1266
           -726.492          -11330.798         16648.9704           292.8204          -12780.108 -28458.895500000002  293  576 1679 1389  340  548 1764
-423.80879999999996  -7706.971200000001          9949.1392           246.5856  -8998.695899999999         -19713.5397  208  405 1157  843  197  372 1067
 -541.3104000000001          -8553.6856         14814.2472           277.4088 -10682.625600000001         -27692.9172  270  480 1636 1145  237  412 1587
          -152.3784 -2849.3954000000003           3532.966            30.8232          -3066.5943          -6664.4352   48  137  388  387   67  137  386
 -574.1135999999999         -13122.9202 17007.329199999996           220.8996         -13770.1368 -29710.533600000002  258  620 1752 1165  273  631 1820
 -719.9351999999999         -13055.4122 17082.997199999998           303.0948 -14398.353000000001 -27601.000200000002  306  646 1642 1141  309  629 1800
          -393.6396  -7251.342000000001          9956.3252           138.7044          -7850.1447 -18064.126800000002  164  353 1060  799  157  348 1078
          -352.6728  -7481.725200000001          9353.2544           143.8416          -8308.0971 -19014.621300000003  149  373 1117  821  149  360 1012
-501.89399999999995  -8072.732600000001           16509.71 308.23199999999997         -10207.4265 -39080.253600000004  263  459 2304  737  252  389 1756
-304.06559999999996  -5980.908200000001  7193.147199999999           113.0184  -6841.034100000001 -12778.507800000001  140  307  750  510  137  287  773
-307.12919999999997 -6615.1864000000005          8618.6956           159.2532          -5697.9882         -14055.1326  129  258  834  799  146  322  902
          -817.1868         -15382.0326 26738.101199999997           369.8784 -16978.006800000003  -55670.18490000001  392  764 3273 1296  370  741 2868
 -489.0791999999999         -10842.5346         12833.9116           190.0764 -12653.875800000002         -22662.0774  243  566 1322  894  211  519 1403
          -126.2628          -2101.7366          3104.4572             25.686 -2563.8741000000005  -6809.105700000001   42  115  397  358   54  101  337
 -706.9331999999999 -10480.033000000001 16605.576399999998           369.8784         -12531.3192         -25137.9756  356  572 1524  765  358  511 1694
          -557.8428         -10676.6954 14539.092799999999           303.0948         -11667.5172 -25653.646800000002  277  524 1508 1011  247  515 1564
          -605.2356         -10859.0328         15159.8296           303.0948         -12743.7795 -25580.750400000004  285  573 1516 1016  294  522 1607
          -260.5956 -4481.1777999999995          6588.3056  97.60679999999999          -4619.8908 -10776.579300000001  107  208  637  551  125  217  700
 -517.6979999999999          -9662.3308 14692.271999999999           220.8996         -10970.3142         -26157.3516  211  494 1548 1596  228  466 1569
-205.86120000000003  -5751.973800000001          5559.7116           113.0184          -6004.3905          -9111.2022  123  271  534  250   95  279  591
-382.41239999999993  -8244.795600000001 10752.457999999999           164.3904          -8256.7251         -19297.2969  214  373 1137  798  176  396 1150
          -355.7364          -6116.6486          9292.2668           148.9788  -7724.280600000001 -17766.315899999998  165  346 1043  649  158  293 1003
            -3.1944             -209.11          1017.1048                  0 -201.45510000000002  -5955.795000000001    1    9  354  218    1   10  110
          -467.7084          -9181.3778         12533.2768           267.1344         -10775.8314          -22351.212  226  486 1312  979  222  443 1346
-141.15119999999996          -1548.5006          3350.2088            102.744 -1984.0959000000003         -18261.5337   82   93 1121   71   86   77  325
-240.21479999999997           -3054.817 10315.989999999998           220.8996          -3619.9557         -21480.3918  144  167 1302  205  154  151  968
          -591.7296          -10770.976          18016.692           241.4484 -12893.494499999999         -32968.5381  262  579 1949 1691  256  520 1911
-126.82319999999999 -2180.6056000000003          3135.9392             51.372          -2006.1117          -6258.7431   49   91  371  358   59  106  328
          -189.8892  -4429.081400000001         11560.3884  77.05799999999999          -4990.1427 -25716.804300000003   77  225 1543  981   90  215 1170
-234.47999999999996           -4444.493          6899.6984           133.5672          -5184.6255 -12994.399800000001  118  233  770  788  112  215  727
           -610.074 -11517.548200000001         17034.1272           395.5644         -13384.1052         -32264.9055  344  600 1895 1346  273  553 1836
          -763.9656         -13044.7756         18362.9028            282.546         -13685.3703         -34008.1695  320  613 2007 1576  326  628 1977
          -543.1224 -10644.785600000001         14768.8464             256.86         -12144.5514         -27650.4381  263  546 1621 1175  244  512 1599
-396.01200000000006           -7183.834 10361.577599999999           190.0764          -7751.8017         -20942.5482  191  347 1226  913  169  346 1125
          -755.9328 -16098.506000000001         24034.4484 390.42719999999997         -18939.7188         -41241.1419  358  856 2451 2712  346  776 2514
           -305.448          -6833.8464  9369.427599999999 195.21359999999999          -7600.6197         -16784.0226  185  343  994 1078  131  330  986
-191.27159999999998 -3168.9220000000005          5766.0336            102.744           -3555.738         -10793.7954   93  160  638  748   84  154  603
          -317.1048  -6310.347000000001  9301.882399999999             128.43 -6834.0617999999995 -19608.162300000004  154  306 1147  796  125  303 1012
-2398.0775999999996          -46202.607         65712.2108          1114.7724 -51493.249800000005        -121851.4725 1180 2314 7201 5115 1082 2223 7018
          -830.9544 -15579.419400000003 22895.513199999998           349.3296         -17411.7402         -42447.6675  389  782 2499 1742  392  747 2462
          -536.6028  -9510.816600000002         17599.2916           303.0948         -10856.5596 -28421.130600000004  260  488 1678 1088  250  459 1874
-1108.6427999999999         -20626.2808 30155.093599999997           446.9364 -22730.669100000003         -49377.4155  468 1021 2923 2171  532  994 3193
           -876.498 -18910.854400000004 25986.359999999997           431.5248         -19577.8413         -47686.6287  404  879 2823 2770  395  910 2751
-316.02119999999996          -5433.5338          7448.1336           184.9392          -6329.8746         -13828.2669  158  286  825  774  152  265  783
-337.69079999999997          -4962.1308  8276.479599999999           174.6648           -7011.666 -15251.513400000002  175  316  902  788  162  240  875
-240.99960000000002          -5929.8984  7912.324799999999            179.802          -6958.4589 -14022.781200000001  133  315  836  983  106  288  828
 -708.2783999999999         -11644.1008         19460.3596           246.5856 -13777.109100000001         -33443.5005  235  621 1993 1629  315  562 2042
-1070.1419999999998 -18911.216600000003 29316.092399999998           416.1132         -20325.3174 -52226.541900000004  444  914 3087 2557  491  911 3114
-466.45680000000004          -9311.6188         14435.6176           164.3904         -10968.4791 -29937.675600000002  193  493 1764 1534  220  448 1535
          -312.3972  -7958.627600000001         12627.7228           174.6648  -8468.822699999999         -25233.0957  138  385 1497 1348  138  386 1319
           -149.184          -3550.4572          4428.7544  97.60679999999999          -3641.9715          -8588.3301   75  165  509  595   66  172  469
           -270.048  -6233.289000000001          9267.1672           164.3904 -7967.5695000000005          -15977.223  146  361  954 1016  136  303  956
-153.06959999999998           -3152.786           4689.434            61.6464          -3226.9518  -8032.277700000001   65  146  477  567   64  152  487
            -898.56          -16876.988 22523.791599999997           380.1528 -18641.387700000003         -40804.3674  395  839 2394 1968  381  812 2417
-453.41759999999994          -8694.5632          11723.606            179.802 -10011.108600000001         -22158.5022  210  450 1302 1229  232  418 1264
 -600.3599999999999 -14314.485000000002 21145.795599999998           292.8204         -17044.7904  -41495.09220000001  326  764 2438 2132  278  687 2270
          -615.0804 -10833.346800000001         18075.0748           287.6832         -12339.4023 -32253.232500000002  272  557 1917 1605  281  522 1901
          -159.3276          -3748.9306          5775.9272           138.7044          -4563.0135         -10931.2479  105  205  643  691   74  181  616
end
------------------ copy up to and including the previous line ------------------