Announcement

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

  • filling missing values

    Dear All, I find this question here (in Chinese). The raw data is
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(stkcd time)
    2002 .
    2002 0
    2002 .
    2002 .
    2002 .
    2002 .
    2002 .
    2002 .
    2002 .
    2002 .
    2004 .
    2004 .
    2004 .
    2004 .
    2004 .
    2004 .
    2004 0
    2004 .
    2004 .
    2004 .
    2006 .
    2006 .
    2006 .
    2006 .
    2006 .
    2006 .
    2006 .
    2006 .
    2006 0
    2006 .
    end
    The end is to, by each `stkcd' and according to the position of `0' of `time', to fill a sequence as
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(stkcd time)
    2002 -1
    2002 0
    2002 1
    2002 2
    2002 3
    2002 4
    2002 5
    2002 6
    2002 7
    2002 8
    2004 -6
    2004 -5
    2004 -4
    2004 -3
    2004 -2
    2004 -1
    2004 0
    2004 1
    2004 2
    2004 3
    2006 -8
    2006 -7
    2006 -6
    2006 -5
    2006 -4
    2006 -3
    2006 -2
    2006 -1
    2006 0
    2006 1
    end
    Any suggestion is appreciated.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    Code:
    gen order = _n                                    // store the original sort order
    bysort stkcd (order) : gen base = _n if time == 0 // find the index of 0
    bysort stkcd (base)  : replace base = base[1]     // copy that index to all rows
    bysort stkcd (order) : replace time = _n - base   // create the variable we want
    drop base order                                   // drop helper variables
    
    list, sepby(stkcd)                                // admire the result
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Dear Maarten, Thanks for this helpful suggestion.

      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment


      • #4
        Maarten Buis , I love the "admire the result..." comment in the last line of your code!

        Comment

        Working...
        X