Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Replace missing values in group data

    Hi everyone,

    Was looking for a solution to the following problem.

    I have a panel data that has some missing values as shown below.

    Code:
    Year  HoustId MedSalary
    2002 1   .
    2002 1   .
    2002 1   .
    2002 1   .
    2002 1   .
    2002 1   .
    2002 1   .
    2002 1   .
    2002 1   23
    2002 1   23
    2002 1   23
    2002 1   23
    How can I replace the missing values with the actual group values that has the result data as shown below?
    Code:
    Year  HoustId MedSalary
    2002 1   23
    2002 1   23
    2002 1   23
    2002 1   23
    2002 1   23
    2002 1   23
    2002 1   23
    2002 1   23
    2002 1   23
    2002 1   23
    2002 1   23
    2002 1   23
    The data is sorted by Year, HousetId.

    In short, I need the entire (Year HoustId) to have the same non-missing (actual) MedSalary.

    Thanks in advance,

    J
    Last edited by Jason Cruso; 22 May 2021, 12:54.

  • #2
    Code:
    bysort Year HoustId (MedSalary): replace MedSalary = MedSalary[1]
    In Stata, the missing value indicator . is a very large numeric value, and such values sort to the end. Therefore, within groups defined by Year and HoustID, and sorted within those groups by MedSalary, the first observation within the group (i.e. [1]) will hold the nonmissing value of MedSalary, if such is available.

    The bysort command is one of the most powerful features of Stata, so checking out the PDF manual entry available via -help bysort- will pay off.

    Comment


    • #3
      Thanks a ton Mike! The code works....

      Comment

      Working...
      X