I am trying to use a loop in stata to achieve the following:

My example data is below, but a brief decsription:

id = ID, date =mofd(date variable), month = 1-12, the number corresponding to what month of the year it is, rank = some integer from 1-10, that is prescribed every 6 months.

Example data:

Code:
clear
input byte id float date byte(month) byte(rank)
1  324  1   .
1  325  2   .
1  326  3   .
1  327  4   .
1  328  5   .
1  329  6   4
1  330  7   .
1  331  8   .
1  332  9   .
1  333  10  .
1  334  11  .
1  335  12  .
1  336  1   .
1  337  2   .
1  338  3   .
1  339  4   .
1  340  5   .
1  341  6   7
1  342  7   .
1  343  8   .
1  344  9   .
1  345  10  .
1  347  11  .
1  348  12  .
2  326  3   .
2  327  4   .
2  328  5   .
2  329  6   9
2  330  7   .
2  331  8   .
2  332  9   .
2  333  10  .
2  334  11  .
2  335  12  .
2  336  1   .
2  337  2   .
2  338  3   .
2  339  4   .
2  340  5   .
2  341  6   .
2  342  7   .
2  343  8   .
2  344  9   .
2  345  10  .
2  347  11  .
2  348  12  .
end
It is also xtset:
Code:
 xtset id date
In my output, I am trying to get the following to happen:

After a rank has been prescribed, I want to copy that value down for the 11 periods below, until we get to the new rank value at month = 6.

The tricky part is dealing with the case where "rank" = " . " when month ==6, because then my code is incorrectly carrying over the previous rank, when it should just be assigning " . " This can be seen for ID = 2, date = 341, the rank = .

My required output would then look something like this.


Code:
clear
input byte id float date byte(month) byte(rank)  byte(continued_rank)
1  324  1   .  .
1  325  2   .  .
1  326  3   .  .
1  327  4   .  .
1  328  5   .  .
1  329  6   4  4
1  330  7   .  4
1  331  8   .  4
1  332  9   .  4
1  333  10  .  4
1  334  11  .  4
1  335  12  .  4
1  336  1   .  4
1  337  2   .  4
1  338  3   .  4
1  339  4   .  4
1  340  5   .  4
1  341  6   7  7
1  342  7   .  7
1  343  8   .  7
1  344  9   .  7
1  345  10  .  7
1  347  11  .  7
1  348  12  .  7
2  326  3   .  .
2  327  4   .  .
2  328  5   .  .
2  329  6   9  9
2  330  7   .  9
2  331  8   .  9
2  332  9   .  9
2  333  10  .  9
2  334  11  .  9
2  335  12  .  9
2  336  1   .  9
2  337  2   .  9
2  338  3   .  9
2  339  4   .  9
2  340  5   .  9
2  341  6   .  .
2  342  7   .  .
2  343  8   .  .
2  344  9   .  .
2  345  10  .  .
2  347  11  .  .
2  348  12  .  .
end

What I tried was:
Code:
gen continued_rank = rank
forvalues 1=1/12{
replace continued_rank = L`i'.rank if continued_rank == .
}
but this returns the exact same output as the "rank" column, without carrying anything over.

I feel like I am not sure what the rule of
Code:
 forval i=1/12{ ... }
is when it comes to panel data. Am I wrong in saying that
Code:
 forval i = 1/10{...}
or any other "1/x" would give me the same output?

A caveat is that the FIRST observation of a certain ID may not start with month == 1. In the above example it starts at month ==3, for id ==2.

Any help will be greatly appreciated.

Moreover, I was also wondering if anyone had a link to practice examples or guides on looping in panel data, and using conditional statements (if, else if etc), since I don't feel like I quite get the hang of it yet.