Announcement

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

  • Difference between previous period and current with panel set

    Hi,

    I have a panel which observes firms over a 7 year period from 2015 to 2021.
    I want to create a new variable (EmployeeChange) which is the difference between that period and the last.

    To give an example of what I want
    Serial Year NumEmployee EmployeeChange
    1 2015 5 .
    1 2016 10 5
    1 2017 12 2
    etc.

    My code is as follows:
    Code:
    xtset Serial Year
    bys Serial Year: gen EmployeeChange = NumEmployee[_n]-NumEmployee[_n-1]
    However it doesn't do anything.

    Any help much appreciated.

  • #2
    if I understand you correctly, the problem is that in your "bys" statement, Year is not in parens and it needs to be; as currently written, and based on the minimal and unusable example (please read the FAQ), this will do nothing; see
    Code:
    help by

    Comment


    • #3
      Originally posted by Rich Goldstein View Post
      if I understand you correctly, the problem is that in your "bys" statement, Year is not in parens and it needs to be; as currently written, and based on the minimal and unusable example (please read the FAQ), this will do nothing; see
      Code:
      help by
      Thanks for your help Rich, the parentheses worked perfectly.

      Comment


      • #4
        Given your xtset

        Code:
        gen wanted = D.NumEmployee
        automatically works panelwise, is shorter, and is smarter about gaps.

        Code:
        clear 
        input Serial    Year    NumEmployee    EmployeeChange
        1    2015    5    .
        1    2016    10    5
        1    2017    12    2
        end 
        
        xtset Serial Year 
        gen wanted = D.NumEmployee
        
        list 
        
             +----------------------------------------------+
             | Serial   Year   NumEmp~e   Employ~e   wanted |
             |----------------------------------------------|
          1. |      1   2015          5          .        . |
          2. |      1   2016         10          5        5 |
          3. |      1   2017         12          2        2 |
             +----------------------------------------------+
        Last edited by Nick Cox; 02 Feb 2024, 04:27.

        Comment

        Working...
        X