Announcement

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

  • Filling missing data from the same variable

    I have a dataset as such:

    Code:
    uid  colour type age
    1    white  small 
    1                10
    2    black        8
    2            big
    For each uid, there are missing data for variables colour, type and age. How can I autofill the missing data under the same uid? The reason of replicating is because there's another variable which is different for each uid, but the three variables presented are the same for each uid.

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte uid str5(colour type) byte age
    1 "white" "small"  .
    1 ""      ""      10
    2 "black" ""       8
    2 ""      "big"    .
    end
    
    sort uid, stable
    foreach v of varlist colour type age {
        by uid: gen _`v' = `v' if _n == 1
        by uid: replace _`v' = cond(missing(_`v'[_n-1]), `v', _`v'[_n-1] ) if _n > 1
        by uid: replace `v' = _`v'[_N]
        drop _`v'
    }
    Note: This code does not check that all non-missing values of these variables are the same for any uid. If that is not the case, all values of the variable will get replaced by the first non-missing value of that variable encountered in the current sort order.

    In the future, please use the -dataex- command to show example data, as I have here. It took me longer to wrestle your tableau into Stata than it took me to write and check the code. -dataex- will also save you time compared to setting up this kind of tableau. It's a win-win! If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte uid str5(colour type) byte age
      1 "white" "small" .
      1 "" "" 10
      2 "black" "" 8
      2 "" "big" .
      end
      
      sort uid, stable
      foreach v of varlist colour type age {
      by uid: gen _`v' = `v' if _n == 1
      by uid: replace _`v' = cond(missing(_`v'[_n-1]), `v', _`v'[_n-1] ) if _n > 1
      by uid: replace `v' = _`v'[_N]
      drop _`v'
      }
      Note: This code does not check that all non-missing values of these variables are the same for any uid. If that is not the case, all values of the variable will get replaced by the first non-missing value of that variable encountered in the current sort order.

      In the future, please use the -dataex- command to show example data, as I have here. It took me longer to wrestle your tableau into Stata than it took me to write and check the code. -dataex- will also save you time compared to setting up this kind of tableau. It's a win-win! If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.
      thanks Clyde! Am learning how to post with -dataex-, thanks for your feedback!

      Comment


      • #4
        You may also like fillmissing from SSC, that has some extra bells and whistles
        Code:
        ssc install fillmissing
        
        help fillmissing
        Regards
        --------------------------------------------------
        Attaullah Shah, PhD.
        Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
        FinTechProfessor.com
        https://asdocx.com
        Check out my asdoc program, which sends outputs to MS Word.
        For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

        Comment

        Working...
        X