Working with missing data in Stata

Q: How do I fill missing data using Stata?

Data1 is how my data looks. Data2 is how I want to make it look.

DATA1

level1 level2 measure

A . 12
. 14
. 45
B . 12
. 23
. 34
DATA2

level1 level2 measure

A 1 12
A 2 14
A 3 45
B 1 12
B 2 23
B 3 34

 

A: Here’s the Solution:


clear
input str1 level1 level2 measure

A . 12
"" . 14
"" . 45
B . 12
"" . 23
"" . 34
end

replace level1 = level1[_n-1] if level1 ==""
bysort level1: replace level2 = _n