Announcement

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

  • Creating lagged and lead variables in unbalanced panel dataset

    Hello,

    I have an unbalanced panel dataset and I need to create lagged (t-3, t-2 and t-1) and lead variables (t+1, t+2, and t+3).

    One of my variables of interest is "interlocked_firm", for which I ran the below code:

    gen lag1 = interlocked_firm[_n-1]

    However, it took into consideration the previous observation regardless of it belonging to another companyid.

    1. What is the right command to use to generate lagged variables?
    2. What is the right command to use to generate lead variables?



    This is the output with the WRONG lag1 variable:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int Year long companyid float(interlocked_firm lag1)
    2009  33 0 .
    2010  33 0 0
    2011  33 0 0
    2012  33 0 0
    2013  33 0 0
    2014  33 1 0
    2015  33 1 1
    2016  33 1 1
    2017  33 1 1
    2018  33 1 1
    2019  33 1 1
    2007  34 1 1
    2008  34 1 1
    2009  34 1 1
    2010  34 1 1
    2011  34 1 1
    2012  34 1 1
    2013  34 1 1
    2014  34 1 1
    2015  34 1 1
    2016  34 1 1
    2017  34 1 1
    2018  34 1 1
    2019  34 1 1
    2015  55 0 1
    2016  55 0 0
    2017  55 0 0
    2018  55 0 0
    2019  55 0 0
    2014 177 1 0
    2015 177 1 1
    2007 217 1 1
    2008 217 1 1
    2009 217 1 1
    2010 217 1 1
    2011 217 1 1
    2012 217 1 1
    2013 217 1 1
    2014 217 1 1
    2015 217 1 1
    2016 217 1 1
    2017 217 1 1
    2018 217 1 1
    2019 217 1 1
    2007 282 1 1
    2008 282 1 1
    2009 282 1 1
    2010 282 1 1
    2011 282 1 1
    2012 282 1 1
    2013 282 1 1
    2014 282 1 1
    2015 282 1 1
    2016 282 1 1
    2017 282 1 1
    2018 282 1 1
    2019 282 1 1
    2009 304 1 1
    2010 304 1 1
    2011 304 1 1
    2012 304 1 1
    2013 304 1 1
    2014 304 1 1
    2015 304 1 1
    2016 304 1 1
    2017 304 1 1
    2018 304 1 1
    2019 304 1 1
    2007 335 1 1
    2008 335 1 1
    2009 335 1 1
    2010 335 1 1
    2011 335 1 1
    2012 335 1 1
    2013 335 1 1
    2014 335 1 1
    2015 335 1 1
    2016 335 1 1
    2017 335 1 1
    2018 335 1 1
    2019 335 1 1
    2008 383 1 1
    2009 383 1 1
    2010 383 1 1
    2011 383 1 1
    2012 383 1 1
    2013 383 1 1
    2014 383 1 1
    2015 383 1 1
    2016 383 1 1
    2017 383 1 1
    2018 383 1 1
    2019 383 1 1
    2015 441 1 1
    2016 441 1 1
    2017 441 1 1
    2018 441 1 1
    2019 441 1 1
    2008 606 1 1
    2009 606 1 1
    end

    This is the -dataex- output for Year, companyid and the interlocked_firm (the variable I want to lag and to lead).

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int Year long companyid float interlocked_firm
    2009  33 0
    2010  33 0
    2011  33 0
    2012  33 0
    2013  33 0
    2014  33 1
    2015  33 1
    2016  33 1
    2017  33 1
    2018  33 1
    2019  33 1
    2007  34 1
    2008  34 1
    2009  34 1
    2010  34 1
    2011  34 1
    2012  34 1
    2013  34 1
    2014  34 1
    2015  34 1
    2016  34 1
    2017  34 1
    2018  34 1
    2019  34 1
    2015  55 0
    2016  55 0
    2017  55 0
    2018  55 0
    2019  55 0
    2014 177 1
    2015 177 1
    2007 217 1
    2008 217 1
    2009 217 1
    2010 217 1
    2011 217 1
    2012 217 1
    2013 217 1
    2014 217 1
    2015 217 1
    2016 217 1
    2017 217 1
    2018 217 1
    2019 217 1
    2007 282 1
    2008 282 1
    2009 282 1
    2010 282 1
    2011 282 1
    2012 282 1
    2013 282 1
    2014 282 1
    2015 282 1
    2016 282 1
    2017 282 1
    2018 282 1
    2019 282 1
    2009 304 1
    2010 304 1
    2011 304 1
    2012 304 1
    2013 304 1
    2014 304 1
    2015 304 1
    2016 304 1
    2017 304 1
    2018 304 1
    2019 304 1
    2007 335 1
    2008 335 1
    2009 335 1
    2010 335 1
    2011 335 1
    2012 335 1
    2013 335 1
    2014 335 1
    2015 335 1
    2016 335 1
    2017 335 1
    2018 335 1
    2019 335 1
    2008 383 1
    2009 383 1
    2010 383 1
    2011 383 1
    2012 383 1
    2013 383 1
    2014 383 1
    2015 383 1
    2016 383 1
    2017 383 1
    2018 383 1
    2019 383 1
    2015 441 1
    2016 441 1
    2017 441 1
    2018 441 1
    2019 441 1
    2008 606 1
    2009 606 1
    end

    Thank you!

  • #2
    Since you have panel data, see

    Code:
    help tsvarlist
    Code:
    xtset company year
    *LAG
    gen wanted1= L.interlocked_firm
    *LEAD
    gen wanted2= F.interlocked_firm
    But otherwise, you want to define groups by company.

    Code:
    bys company (year): gen wanted3= interlocked_firm[_n-1]
    This however will not generate the correct results if your panel is unbalanced with gaps.
    Last edited by Andrew Musau; 03 May 2023, 16:29.

    Comment


    • #3

      Thank you for your reply Andrew.

      Please note that I got this error "repeated time values within panel, r(451)"

      when I ran this command: xtset companyid Year

      What should I do?

      Last edited by Christelle Alkhoury; 03 May 2023, 16:15.

      Comment


      • #4
        Thank you for your reply Andrew.

        Please note that I got this error "repeated time values within panel, r(451)"

        when I ran this command: xtset companyid Year

        What should I do?

        Comment


        • #5
          Check why you have duplicates of company and year.

          Code:
          bys companyid Year: gen tag=_N>1
          list if tag, sepby(companyid Year)

          Comment


          • #6
            Thank you. I found 3 duplicates due to a difference in company name. Now all good.
            Just one last question, the commands below would provide the lag for t-1 and lead for t+1. How can I also get lag and lead for t+2, t+3, t-2 and t-3?
            I want a window of -3 years and +3 years.

            *LAG

            gen wanted1= L.interlocked_firm

            *LEAD

            gen wanted2= F.interlocked_firm
            Last edited by Christelle Alkhoury; 03 May 2023, 16:41.

            Comment


            • #7
              Explained in

              Code:
              help tsvarlist
              as direted in #2.

              Comment


              • #8
                Perfect! Thank you!!

                Comment

                Working...
                X