I have a dataset that looks like the following:
id test
1 A
2 A
3 A
. B
. B
. B
I would like to fill in the missing values with a sequence of integers corresponding to their id
:
id test
1 A
2 A
3 A
1 B
2 B
3 B
I initially thought about using a forvalues
loop as follows:
forvalues i=1/3 {
replace id = `i' if (id == .)
}
But that (for obvious reasons) just replaces all the missing values with 1
during the first iteration.
I then thought about restricting the loop to a subset of the data by row indexing (like in R), but this functionality doesn't seem to exist for Stata(?):
forvalues i=1/3 {
replace id[3+`i'] = `i' if (id == .)
}
How can I go about this seemingly simple task in Stata?