I would like to replace NA with previous value in an unbalanced panel data (long format). Before replace, the data would look like
Firm    Date    Var_1       
AAA 19990430    NA      
AAA 19990531    10      
AAA 19990630    NA      
AAA 19990731    NA      
AAA 19990831    12      
AAA 19990930    NA      
BBB 20040331    NA      
BBB 20040430    NA      
BBB 20040531    8       
BBB 20040630    NA      
BBB 20040731    NA      
BBB 20040831    12      
BBB 20040930    NA      
After, it will look like:
Firm    Date    Var_1       
AAA 19990430    NA      
AAA 19990531    10      
AAA 19990630    10      
AAA 19990731    10      
AAA 19990831    12      
AAA 19990930    12      
BBB 20040331    NA      
BBB 20040430    NA      
BBB 20040531    8       
BBB 20040630    8       
BBB 20040731    8       
BBB 20040831    12      
BBB 20040930    12
The point is the two NAs at the beginning of Firm BBB shall not be replaced by the numbers from firm AAA. Thanks in advance.
 
     
    