Announcement

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

  • Panel data: difference between two annual observations

    Hi!
    I have a panel dataset of companies, with observations for Sales for each year. I would like to generate a new variable (delta_2005-2001) displaying the difference in Sales for company 1 between year 2005 and 2001, and similarly the difference for company 2, 3, etc. Some companies have missing values, so I cannot write n-5 or the like. I have made an example of my dataset below. I tried this command:
    egen delta_2005-2001 = Sales if Year==2005 - Sales if Year==2001 , by(Company)
    But that did not work.
    Do you have any suggestions? Any help would be greatly appreciated!

    Click image for larger version

Name:	Screen Shot 2019-02-09 at 6.16.53 PM.png
Views:	2
Size:	50.1 KB
ID:	1482865

    Attached Files
    Last edited by Johanne Buten; 09 Feb 2019, 10:31.

  • #2
    Welcome to Statalist.

    What you want to do is make use of Stata's time series operators. To do this, you start by declaring your data to be panel data, with Company identifying the panel and Year identifying the time,
    Code:
    xtset Company Year
    Then you want to make use of Stata's time series notation (see the output of help tsvarlist), which takes into account missing observations.
    Code:
    generate delta_2005_2001 = Sales - L4.Sales if Year==2005
    Now, lacking actual data I have to say that this code has not been tested. I'll close with some brief advice on making effective use of Statalist.

    Take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

    Comment


    • #3
      Wow, thank you very much, Mr. Lisowski!! The code worked! And thank you for the brief advice.
      Best,
      Johanne

      Comment

      Working...
      X