Announcement

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

  • Fill null values

    Hello. For a data structure like the one in the figure, sorted in ascending order by "numdom" within the commune-street group, I need to fill the null values of "uv" as long as the condition that the previous value of "uv "match the next non-null. In my example, observations 4 and 5 should be filled with the value uv = "20A", but observations 9 and 10 should not, since "20B"! = "20C". How could I do it? Thank you
    Click image for larger version

Name:	figurass.png
Views:	1
Size:	17.0 KB
ID:	1613929

  • #2
    Try this (hopefully no errors):

    Code:
    preserve
    tempfile temp_file
    drop if uv==""
    gen next_is_same=uv==uv[_n+]
    keep comuna numdom same
    save `temp_file'
    restore
    merge 1:1 comuna numdom using `temp_file', keep(1 3) nogen
    replace same=same[_n-1] if same==. 
    replace uv=uv[_n-1] if uv=="" & same[_n-1]==1

    Comment


    • #3
      It did not work. It filled in values that should have remained null :-(

      Thank you anyway

      Comment


      • #4
        I saw an error (fixed below). Give it another go?

        Code:
        preserve
        tempfile temp_file
        drop if uv==""
        gen next_is_same=uv==uv[_n+1] // FIXED
        keep comuna numdom same
        save `temp_file'
        restore merge 1:1 comuna numdom using `temp_file', keep(1 3) nogen
        replace same=same[_n-1] if same==.
        replace uv=uv[_n-1] if uv=="" & same[_n-1]==1

        Comment


        • #5
          I had already detected that notation error and had corrected it before ...

          Comment


          • #6
            I believe this will do what you want:

            Code:
            gen uvmiss = uv!=""
            bysort comuna calle (uvmiss numdom): gen nextnonmiss = cond(_n!=_N,uv[_n+1],"N/A") if uv!=""
            bysort comuna calle (numdom): replace nextnonmiss = nextnonmiss[_n-1] if nextnonmiss==""
            by comuna calle: replace uv = uv[_n-1] if uv=="" & nextnonmiss==uv[_n-1]
            drop nextnonmiss uvmiss
            Last edited by Ali Atia; 09 Jun 2021, 12:53.

            Comment

            Working...
            X