Announcement

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

  • lead operators

    Hi,
    I am trying to create a variable using a lead operator for data that is a monthly panel. The variable I am trying to get the lead for is measured at the yearly level though. To get the first and second leads, I am using the following code:
    Code:
    xtset id mdate
    set more off
    foreach var of varlist var1 var2 {
        g first`var' = `var'==0 & F12.`var'==1 if F12.`var'!=.
        g second`var' = `var'==0 & F24.`var'==0 & F24.`var'==1 if F24.`var'!=. & F24.`var'!=.
    }
    Is this the best/most efficient approach to create this operator?

  • #2
    Never mind efficiency, let's get it correct first. Your -g second`var'...- command can't be right. Since it includes F24.`var' == 0 & F24.`var' == 1, you will always get 0 for second`var' (except where it will be missing), because F24.`var' can never be both 0 and 1.

    Since I don't know what you intended to do for second`var', I can't specify how to correct this.

    Comment


    • #3
      Sorry, it should be:

      Code:
       xtset id mdate set more off foreach var of varlist var1 var2 {    
      g first`var' = `var'==0 & F12.`var'==1 if F12.`var'!=.    
      g second`var' = `var'==0 & F12.`var'==0 & F24.`var'==1 if F24.`var'!=. & F24.`var'!=. }
      For the second var, I'm trying to code the variable as one if if the two year lead of the variable is one and the 1-year lead and variable itself are both 0.
      Last edited by Adrienne Wold; 04 Dec 2021, 16:15.

      Comment


      • #4
        Code:
        gen second_`var'= F24.`var' == 1 & F12.`var' == 0 & `var' == 0 if !missing(`var', F12.`var', F24.`var')
        should do it.

        Comment


        • #5
          Thanks. Although I should be using 13 and 26 instead?

          Comment


          • #6
            No. If you have monthly data and you want to look 1 and 2 years ahead, then you look at F12. and F24., respectively.

            Comment

            Working...
            X