Announcement

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

  • Create dummy variable at year t based on information of year (t+1)

    Dear everyone,

    For example, I have panel data set like this:
    Code:
    clear
    input double person_id int year str3 same_employer
    1 2000 "Yes"
    1 2001 "Yes"
    1 2002 "No"
    2 2000 "No"
    2 2001 "Yes"
    2 2002 "No"
    3 2000 "No"
    3 2001 "No"
    4 2000 "Yes"
    end
    I want to create a dummy variable named "quit" for year t based on the variable "same_employer" in the year (t+1). If the variable "same_employer" takes "No" in the year (t+1), then the "quit" variable at year t takes a value of 1.

    For example, for person ID 1, we see that in 2001 his answer is "Yes" for "same_employer", then his "quit" variable in 2000 would be 0.

    Here is the code that I have used:
    Code:
    gen quit=0
    bysort person_id (year): replace quit=1 if same_employer[_n+1]=="No"
    However, since I am using the information in the year (t+1) to create a dummy for year t; therefore, the final year of each person_id would have a missing value. For example, for person_id 1, the "quit" variable for the year 2002 would have a missing value.

    My question here is, how do I generate a missing value in this case since each person_id has a different final year? The code above does not generate a missing value. Here is an example of what I am looking for:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double person_id int year str3 same_employer double quite
    1 2000 "Yes" 0
    1 2001 "Yes" 1
    1 2002 "No" .
    2 2000 "No" 0
    2 2001 "Yes" 1
    2 2002 "No" .
    3 2000 "No" 1
    3 2001 "No" .
    4 2000 "Yes".
    end
    Thank you for your help. Please also let me know if my code is correct.


  • #2
    Code:
    bysort person_id (year): gen byte quit = cond(same_employer[_n+1] == "No",1,cond(same_employer[_n+1] == "Yes",0,.))
    Last edited by Hemanshu Kumar; 06 Oct 2022, 04:49.

    Comment


    • #3
      Thanks @Hemanshu Kumar a lot.

      Just curious that I wanted to include more conditions, then I would just keep adding -cond()-, right?

      Comment


      • #4
        Yes, you can keep nesting the cond.

        Comment

        Working...
        X