Announcement

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

  • Generating a variable: How is the syntax?

    Hello,

    I have a dataset with observations of different companies. I would like to create a variable "Competitor size" which expresses the following: The total sales of the other companies in the same industry sector (sic). Sales and sic are given for each company per year. I have difficulties, because the sales value of the observed company must not be counted in the calculation.

    It would be great if someone could help me with the syntax

    Thanks in advance, Hanna


  • #2
    Code:
    isid sic year company, sort
    by sic year: egen total_sic_sales = total(sales)
    gen wanted = total_sic_sales - sales
    Note: the above code requires that there be only a single observation for a given company in any given year, and that each company is classified in only a single industry. It also makes a number of other key assumptions about your data that are fairly typical of what is seen, but by no means guaranteed to be true. If those assumptions are wrong, this code will fail and we will have both wasted our time. That could have been avoided by, as the Forum FAQ, which everyone is requested to read before posting, recommends, posting example data with the -dataex- command. 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

    Working...
    X