Announcement

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

  • Forward filling of values

    Hi Stata users,

    I am working with data extracted from a database where some values are missing as shown in example below
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int(id code) byte value
    1000 101 34
       . 102 56
       . 105 96
    2000 100 29
       . 101 36
       . 102 81
       . 103 34
    3000 101 46
       . 102 38
       . 103 78
       . 104 45
       . 105 71
       . 106 76
    end
    Is there a way I can fill the values such that the ID for observations 2 and 3 is 1000, observations 6, 7 and 8 is 2000 etc.

    Thanks in advance!

  • #2
    This is an FAQ

    https://www.stata.com/support/faqs/d...issing-values/

    Comment


    • #3
      Thanks so much Nick Cox for the suggestion. It's greatly appreciated.

      Comment


      • #4
        Nick referred you to a Stata's native solution. If you are comfortable with using user-written programs, you may like to try fillmissing.
        Code:
        ssc install fillmissing
        fillmissing id, with(previous)
        
        list
        
             +---------------------+
             |   id   code   value |
             |---------------------|
          1. | 1000    101      34 |
          2. | 1000    102      56 |
          3. | 1000    105      96 |
          4. | 2000    100      29 |
          5. | 2000    101      36 |
             |---------------------|
          6. | 2000    102      81 |
          7. | 2000    103      34 |
          8. | 3000    101      46 |
          9. | 3000    102      38 |
         10. | 3000    103      78 |
             |---------------------|
         11. | 3000    104      45 |
         12. | 3000    105      71 |
         13. | 3000    106      76 |
             +---------------------+
        On this page https://fintechprofessor.com/2019/12...lues-in-stata/, I show several other examples of filling missing values in variables.
        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


        • #5
          For that matter mipolate as part of mipolate (SSC) will do this too. https://www.statalist.org/forums/for...-interpolation



          In each case the community-contributed command will do more than the one-liner in the FAQ, but if the one-liner suffices there is no need for any such.

          Yet more: carryforward (SSC).
          Last edited by Nick Cox; 09 Jan 2022, 10:51.

          Comment


          • #6
            Thanks Attaullah Shah for the alternative. Will look at the user written command.

            Comment


            • #7
              Thanks Nick Cox for the additional inputs. I agree with your sentiments

              Comment

              Working...
              X