Announcement

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

  • Replace missing values with non-missing value

    Dear Stata users,

    Suppose I have a variable x composed of missing values and non-missing values, and the non-missing values all are equal. I want to replace missing values with (this?) non-missing value, how can I achieve it? Thank you.
    Take an example, I want to replace variable x with 12 if x are missing.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float x
    .
    12
     .
    12
     .
    12
     .
     .
    12
     .
    end

  • #2
    Code
    recode x (.=12)

    Comment


    • #3
      Code:
      . egen newx = max(x)
      
      . list, clean
      
              x   newx  
        1.    .     12  
        2.   12     12  
        3.    .     12  
        4.   12     12  
        5.    .     12  
        6.   12     12  
        7.    .     12  
        8.    .     12  
        9.   12     12  
       10.    .     12
      More generally, suppose this is panel data with a panel identifier variable named id and a time variable named year. Then you may want
      Code:
      by id year, sort: egen newx = max(x)
      which will give the nonmissing value separately for each combination of id and year.

      Comment


      • #4
        Thank you very much William Lisowski. I used summarize to get r(max) and used it in replace expression here. From time to time, I forget that egen function can do the same.
        Last edited by Chen Samulsion; 23 Feb 2022, 22:57.

        Comment

        Working...
        X