Announcement

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

  • Identifying a common observation across the groups by year

    Hi, I have the following kind of data structure. I just want to keep or identify the product codes that are common across all the company codes by year.
    year companycode itemcode
    2002 11 115
    2002 11 117
    2002 11 119
    2002 11 112
    2002 12 117
    2002 12 115
    2002 12 119
    2002 16 108
    2002 16 117
    2002 16 119
    2002 16 111
    2002 16 115
    2002 16 120
    2002 14 222
    2002 14 117
    2002 14 115
    2002 14 119
    2003 11 115
    2003 11 317
    2003 11 119
    2003 11 112
    2003 12 117
    2003 12 115
    2003 12 119
    2003 16 108
    2003 16 117
    2003 16 119
    2003 16 111
    2003 16 115
    2003 16 120
    2003 14 222
    2003 14 117
    2003 14 111
    2003 14 119
    2004 11 115
    2004 11 117
    2004 11 119
    2004 11 112
    2004 12 117
    2004 12 115
    2004 12 119
    2004 16 108
    2004 16 117
    2004 16 119
    2004 16 111
    2004 16 115
    2004 16 120
    2004 14 222
    2004 14 117
    2004 14 115
    2004 14 119

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int year byte companycode int itemcode
    2002 11 115
    2002 11 117
    2002 11 119
    2002 11 112
    2002 12 117
    2002 12 115
    2002 12 119
    2002 16 108
    2002 16 117
    2002 16 119
    2002 16 111
    2002 16 115
    2002 16 120
    2002 14 222
    2002 14 117
    2002 14 115
    2002 14 119
    2003 11 115
    2003 11 317
    2003 11 119
    2003 11 112
    2003 12 117
    2003 12 115
    2003 12 119
    2003 16 108
    2003 16 117
    2003 16 119
    2003 16 111
    2003 16 115
    2003 16 120
    2003 14 222
    2003 14 117
    2003 14 111
    2003 14 119
    2004 11 115
    2004 11 117
    2004 11 119
    2004 11 112
    2004 12 117
    2004 12 115
    2004 12 119
    2004 16 108
    2004 16 117
    2004 16 119
    2004 16 111
    2004 16 115
    2004 16 120
    2004 14 222
    2004 14 117
    2004 14 115
    2004 14 119
    end
    
    isid year companycode itemcode, sort
    
    //    CALCULATE NUMBER OF COMPANIES IN DATA SET IN EACH YEAR
    by year (companycode), sort: gen n_companies = sum(companycode != companycode[_n-1])
    by year (companycode): replace n_companies = n_companies[_N]
    
    //    KEEP ONLY ITEMS THAT ARE FOUND IN EVERY COMPANY FOR THE YEAR
    by year itemcode (companycode), sort: keep if n_companies == _N
    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 18, 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.
    Last edited by Clyde Schechter; 13 Jul 2023, 09:04.

    Comment

    Working...
    X