Announcement

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

  • Counting Observations in Panel Data and filling in missing values

    Hello everyone,
    I am new to STATA and have a question regarding the preparation of my Data sample. "PERMCO" shows the respective firm id, "year" shows the year in which an M&A took place. There is two things i want to do with the data:

    1.: I would like to count the number of observations for each PERMCO per year, so that at the end, for example, for PERMCO "10078" instead of four times, I have the value "2002" listed there only once, and behind it, in a new variable, information is given about the frequency of the observation 2002.

    2.: I would like to add the years missing in the interval 2000-2010 for each PERMCO group. The missing values vary throughout the Dataset and are not the same missing years for every PERMCO. Since no observation has taken place in these missing values, have them output with a number of "0" according to the logic explained in point 1

    I kindly ask for your help in creating a code for these concerns.

    Original File:
    Click image for larger version

Name:	Screenshot Stata Forum.PNG
Views:	2
Size:	6.6 KB
ID:	1638931


    What I would like it to look like (example for PERMCO 10078 representative for the entire data set):

    Click image for larger version

Name:	Screenshot Stata Forum2.PNG
Views:	2
Size:	6.4 KB
ID:	1638932


    I hope someone can help me. With best regards

    Christopher

  • #2
    Something like this:
    Code:
    contract PERMNO Year, freq(Numberofmergers)
    fillin PERMNO Year
    replace Numberofmergers = 0 if _fillin
    Note: Code not tested because example data shown was not importable to Stata. Beware of typos or other errors, but this is the gist of it.

    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Thank you very much for the reply. The Code works well. And also thank you for the advice on posting data examples, I will follow them next time!
      Best regardsI
      Christopher

      Comment

      Working...
      X