Announcement

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

  • Panel Data Cleaning Question: Replacing Variable(s) Based on a Previous Observation

    Hello (and thank you in advance for any help),

    I have a panel dataset (with variable id as the cross-section and time as the time dimension). I have attached example data to this post. The variable Mark currently indicates the first time the id gets a treatment. However, I want to replace "Mark" such that all times after the first treatment (for each id) also receive a "1" and that all values prior to the first treatment get a "0". For example, for id 1 and time 4, there should be a "1" in Mark. For id 2, there should be a "1" in Mark for times 3 and 4 also.

    Can anyone help with generating a code that will solve this problem (as I will apply it to a larger dataset)? Thank you for any help you can provide!

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(id time Mark)
    1 1 .
    1 2 .
    1 3 1
    1 4 .
    2 1 .
    2 2 1
    2 3 .
    2 4 .
    end
    Last edited by Jason Yang; 28 Sep 2022, 10:59.

  • #2
    Code:
    bys id (time): replace Mark= Mark[_n-1] if missing(Mark) & !missing(Mark[_n-1])
    replace Mark= !Mark if missing(Mark)

    Comment


    • #3
      This was exactly what I was looking for, thank you so much!

      Comment


      • #4
        Also note that

        Code:
        bys id (time): replace Mark=sum(Mark)
        will do it as missing values will be summed as zeros.

        Comment


        • #5
          That's a great alternative way. Thank you again!

          Comment

          Working...
          X