Announcement

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

  • How to Replace the Missing Values with a "0" or "1" on a Specific Variable in Long Format Data in Stata?

    Hello, I have a dataset like this,
    clear
    input str10 id byte (state)
    1 1
    1 1
    1 .
    1 .
    1 1
    1 1
    1 1
    1 1
    1 .
    1 0
    1 0
    1 0
    1 0
    2 1
    2 1
    2 1
    2 .
    2 1
    2 1
    2 .
    2 0
    2 0
    2 0
    2 0
    2 0
    2 0
    3 1
    3 1
    3 1
    3 1
    3 1
    3 .
    3 0
    3 .
    3 0
    3 0
    3 0
    3 0
    3 0
    end

    I just want to connect the gaps between 1 and 1 or 0 and 0 on the state variable.
    As for the gaps between "1" and "0", just leave them alone.
    Can someone help me?
    Thank you!
    Last edited by smith Jason; 30 Jul 2022, 13:07.

  • #2
    Can anybody help me? Thank you!

    Comment


    • #3
      Code:
      gen long seq = _n
      
      by id (seq), sort: gen run_num = sum(state != state[_n-1])
      
      frame put id state run_num if run_num != run_num[_n-1], into(singletons)
      frame singletons {
          gen previous = state[_n-1]
          gen next = state[_n+1]
      }
      
      frlink m:1 id run_num, frame(singletons)
      frget previous next, from(singletons)
      
      replace state = previous if missing(state) & previous == next

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        Code:
        gen long seq = _n
        
        by id (seq), sort: gen run_num = sum(state != state[_n-1])
        
        frame put id state run_num if run_num != run_num[_n-1], into(singletons)
        frame singletons {
        gen previous = state[_n-1]
        gen next = state[_n+1]
        }
        
        frlink m:1 id run_num, frame(singletons)
        frget previous next, from(singletons)
        
        replace state = previous if missing(state) & previous == next
        Professor Clyde, thank you for your code. I applied your code with the replacement of "state" with "primary" to the attached .dta file (my simulated .dta data) and found that there is still a gap on the variable "primary" between line 15 and line 17 (line 16).


        I don't know why this happens. Could you please take a look at it?
        Thank you!
        Attached Files
        Last edited by smith Jason; 30 Jul 2022, 19:18.

        Comment


        • #5
          I do not open attachments from people I don't know. Please post back with the same data using -dataex-.

          Comment


          • #6
            clear
            input str10 id byte (state)
            1 0
            1 0
            1 0
            1 0
            1 0
            1 0
            1 0
            1 0
            1 0
            1 0
            1 0
            1 0
            1 0
            2 1
            2 1
            2 .
            2 1
            2 1
            2 1
            2 1
            2 1
            2 .
            2 0
            2 0
            2 0
            2 0
            3 1
            3 1
            3 1
            3 1
            3 1
            3 1
            3 .
            3 0
            3 0
            3 0
            3 0
            3 0
            3 0
            4 0
            4 0
            4 0
            4 0
            4 0
            4 0
            4 0
            4 0
            4 0
            4 0
            4 0
            4 0
            4 0
            5 1
            5 1
            5 1
            5 1
            5 1
            5 .
            5 0
            5 0
            5 0
            5 0
            5 0
            5 0
            5 0
            end
            Thank you!

            Comment


            • #7
              Ah, my error in the way I set up frame singletons.

              Code:
              gen long seq = _n
              
              by id (seq), sort: gen run_num = sum(state != state[_n-1])
              
              frame put id state run_num , into(singletons)
              frame singletons {
                 duplicates drop
                  by id (run_num), sort: gen previous = state[_n-1]
                  by id (run_num): gen next = state[_n+1]
              }
              
              frlink m:1 id run_num, frame(singletons)
              frget previous next, from(singletons)
              
              replace state = previous if missing(state) & previous == next
              Changes in italics.
              Last edited by Clyde Schechter; 30 Jul 2022, 20:29.

              Comment


              • #8
                Professor Clyde, Thank you very much!

                Comment

                Working...
                X