Hi,

I am trying to loop through a list of variables to replace some values of variables named "varA" by variables named "var". I have written the following loop that does not do anything (no error):
Code:
foreach var in $income_int $balance_sheet_int $ratio_int {
    replace `var'A=`var' if (dup==1 & missing(`var'A))
    *replace WC02999A=L1.WC02999A if ((L1.dup==1 | L2.dup==1) & missing(fisc_q))
}
I have tried to look at what's going on inside the loop but it seems to work this way:
Code:
global test WC02999
local test1 $test
replace `test1'A=`test1' if (dup==1 & missing( `test1'A ))
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str6 Code double(unit_id WC02999 WC02999A)
"130062" 1  789371       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1  768918       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1 1105713       .
"130062" 1       .       .
"130062" 1       .       .
"130062" 1 1588589 1121719
"130062" 1       . 1121719
"130062" 1       . 1121719
"130062" 1 1588589 1200217
"130062" 1       . 1200217
"130062" 1       . 1200217
"130062" 1 1588589 1236388
"130062" 1       . 1236388
"130062" 1       . 1236388
"130062" 1 1588589 1588589
"130062" 1       . 1588589
"130062" 1       . 1588589
"130062" 1 1742574 1596804
"130062" 1       . 1596804
"130062" 1       . 1596804
"130062" 1 1742574 1668962
"130062" 1       . 1668962
"130062" 1       . 1668962
"130062" 1 1742574 1733474
"130062" 1       . 1733474
"130062" 1       . 1733474
"130062" 1 1742574 1742574
"130062" 1       . 1742574
"130062" 1       . 1742574
"130062" 1 1841439 1766405
"130062" 1       . 1766405
"130062" 1       . 1766405
"130062" 1 1841439 1838118
"130062" 1       . 1838118
"130062" 1       . 1838118
"130062" 1 1841439 1854288
"130062" 1       . 1854288
"130062" 1       . 1854288
"130062" 1 1841439 1841439
"130062" 1       . 1841439
"130062" 1       . 1841439
"130062" 1 2224580 2157119
"130062" 1       . 2157119
"130062" 1       . 2157119
"130062" 1 2224580 2234584
"130062" 1       . 2234584
"130062" 1       . 2234584
"130062" 1 2224580 2258410
"130062" 1       . 2258410
"130062" 1       . 2258410
"130062" 1 2224580 2224580
"130062" 1       . 2224580
"130062" 1       . 2224580
"130062" 1 2258530 2236343
"130062" 1       . 2236343
"130062" 1       . 2236343
"130062" 1 2258530 2301541
"130062" 1       . 2301541
"130062" 1       . 2301541
"130062" 1 2258530 2321994
"130062" 1       . 2321994
"130062" 1       . 2321994
"130062" 1 2258530 2258530
"130062" 1       . 2258530
"130062" 1       . 2258530
"130062" 1 2330093 2268875
"130062" 1       . 2268875
"130062" 1       . 2268875
"130062" 1 2330093 2306973
"130062" 1       . 2306973
"130062" 1       . 2306973
"130062" 1 2330093 2331182
"130062" 1       . 2331182
"130062" 1       . 2331182
"130062" 1       . 2331182
"130062" 1 2330093 2330093
"130062" 1       . 2330093
"130062" 1       . 2330093
end
label values unit_id unit_id
label def unit_id 1 "130062", modify

I am necessarily making a mistake in the way I wrote the loop. Can someone help?