title: Stata: replace missing values with existing observations
body: <p>I am trying to replace missing values with values from the same column dependent on their equality from other columns:</p>
  
  <p>I have different firms, from different industries &amp; countries and from different years. Below is just a small example. I would like to replace the missing values (in the column industry or country) with the existing values in the column, if they come from the same firm.</p>
  
  <p>For example, firm 123 is missing its industry in year 2. I have the industry in which the firm belongs to from the previous (or sometimes succeeding) years but do not know how to add it.</p>
  
  <p>Another example: I have the firm 444 which is missing its country in year 3. I do have its country from the previous years but do not know how to transfer / copy it to the 3rd year:</p>
  
  <pre><code> --firm--year--industry--country
  
   --123----1-------1---------usa
  
   --123----2-------1---------usa
  
   --123----3--------.--------usa
  
   --333----1-------2---------usa
  
   --333----2--------.---------usa
  
   --444---1---------.----------fr
  
   --444---2---------2---------fr
  
   --444---3---------2----------.
  </code></pre>
  
  <p>I looked up on stata/help and on the internet. All I could find was the replace command, but it only replaced equal numbers.</p>
  
  <p>I think it will be something with:</p>
  
  <pre><code>replace industry=(problaby something dependant of the firm (and maybe year))  if industry==.
  
  replace country=(problaby something dependant of the firm (and maybe year))  if country==.
  </code></pre>
  
  <p>I am not sure for the country replacement, because the observations are not numbers. I think I will need to generate a new variable with numbers for the country-replacement.</p>
  
  <p>Thanks a lot!</p>