Hi

For the following dataset, I have generated the first month of the data for X2 and X3 but for remaining months and years need to fill the two variables X2 and X3 . I am using the following commands:

by year (month): replace X2 = X1 / X3[_n-1] if X2 == .

by year month: replace X2 = X2[_n-1] if X2 == .

replace X3 = X1 / X2 if X3 == .

I need to run these commands till all the missing values are filled. Would appreciate if someone could help with how to run these commands in a loop. I tried to run a 'forvalues' loop but couldn't properly adjust the months and years.

Thanks in advance.




Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(year month) double dates float day double X1 float(X2 X3)
2013 6 19510  1  23.8 100  .238
2013 6 19511  2 22.71 100 .2271
2013 6 19512  3 21.34 100 .2134
2013 6 19513  4 20.45 100 .2045
2013 6 19514  5 21.94 100 .2194
2013 6 19515  6 21.83 100 .2183
2013 6 19516  7 22.13 100 .2213
2013 6 19517  8 22.36 100 .2236
2013 6 19518  9  22.6 100  .226
2013 6 19519 10 23.58 100 .2358
2013 6 19520 11  22.7 100  .227
2013 6 19521 12 23.45 100 .2345
2013 6 19522 13 22.25 100 .2225
2013 6 19523 14 22.52 100 .2252
2013 6 19524 15 23.61 100 .2361
2013 6 19525 16 21.88 100 .2188
2013 6 19526 17 21.15 100 .2115
2013 6 19527 18 21.01 100 .2101
2013 6 19528 19 21.14 100 .2114
2013 6 19529 20 24.35 100 .2435
2013 6 19530 21 23.22 100 .2322
2013 6 19531 22 21.09 100 .2109
2013 6 19532 23 21.12 100 .2112
2013 6 19533 24 24.87 100 .2487
2013 6 19534 25 25.45 100 .2545
2013 6 19535 26 26.09 100 .2609
2013 6 19536 27 25.11 100 .2511
2013 6 19537 28 23.26 100 .2326
2013 6 19538 29 21.78 100 .2178
2013 6 19539 30 21.62 100 .2162
2013 7 19540  1 20.85   .     .
2013 7 19541  2 21.77   .     .
2013 7 19542  3 22.37   .     .
2013 7 19543  4 24.43   .     .
2013 7 19544  5 22.66   .     .
2013 7 19545  6  23.8   .     .
2013 7 19546  7 22.86   .     .
2013 7 19547  8 21.84   .     .
2013 7 19548  9 21.68   .     .
2013 7 19549 10 21.49   .     .
2013 7 19550 11 21.59   .     .
2013 7 19551 12 19.96   .     .
2013 7 19552 13  20.5   .     .
2013 7 19553 14  20.3   .     .
2013 7 19554 15 19.94   .     .
2013 7 19555 16 20.04   .     .
2013 7 19556 17 19.27   .     .
2013 7 19557 18 19.84   .     .
2013 7 19558 19 19.59   .     .
2013 7 19559 20 19.46   .     .
2013 7 19560 21  19.2   .     .
2013 7 19561 22 18.42   .     .
2013 7 19562 23 18.93   .     .
2013 7 19563 24 18.16   .     .
2013 7 19564 25 18.46   .     .
2013 7 19565 26 18.15   .     .
2013 7 19566 27 17.77   .     .
2013 7 19567 28 18.48   .     .
2013 7 19568 29 18.13   .     .
2013 7 19569 30  17.7   .     .
2013 7 19570 31 18.73   .     .
2013 8 19571  1 19.16   .     .
2013 8 19572  2  20.2   .     .
2013 8 19573  3 19.78   .     .
2013 8 19574  4 19.13   .     .
2013 8 19575  5 19.61   .     .
2013 8 19576  6 19.47   .     .
2013 8 19577  7 18.19   .     .
2013 8 19578  8  20.3   .     .
2013 8 19579  9 19.42   .     .
2013 8 19580 10 19.82   .     .
2013 8 19581 11 18.11   .     .
2013 8 19582 12 18.43   .     .
2013 8 19583 13 19.29   .     .
2013 8 19584 14  18.3   .     .
2013 8 19585 15 19.77   .     .
2013 8 19586 16 20.28   .     .
2013 8 19587 17 20.77   .     .
2013 8 19588 18 20.95   .     .
2013 8 19589 19 22.14   .     .
2013 8 19590 20 24.05   .     .
2013 8 19591 21 21.91   .     .
2013 8 19592 22 20.06   .     .
2013 8 19593 23 20.07   .     .
2013 8 19594 24 20.19   .     .
2013 8 19595 25 22.56   .     .
2013 8 19596 26 21.94   .     .
2013 8 19597 27 20.39   .     .
2013 8 19598 28 21.56   .     .
2013 8 19599 29 22.41   .     .
2013 8 19600 30 20.72   .     .
2013 8 19601 31 19.35   .     .
2013 9 19602  1 19.19   .     .
2013 9 19603  2  18.5   .     .
2013 9 19604  3  17.7   .     .
2013 9 19605  4 19.24   .     .
2013 9 19606  5 20.05   .     .
2013 9 19607  6 19.58   .     .
2013 9 19608  7 18.23   .     .
2013 9 19609  8  18.9   .     .
end
format %td dates