Announcement

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

  • Keep the observations that are closest to a value

    Hello everyone,

    I have a panel dataset with multiple ID's and Years. So e.g., I have 10 observations for ID_1 in 2014, 8 obs. ID_1 in 2015, etc. I have another variable called "Size_ratio" and I want to keep the best 5 observations per ID and year, meaning the observations for which Size-ratio is closest to "1". Is there any way to do it?

    Many thanks in advance.

    Beyza

  • #2
    Something like this:
    Code:
    gen delta = abs(size_ratio - 1)
    by ID year (delta), sort: keep if inrange(_n, 1, 5)
    As no example data was provided, this code is untested and may contain errors. It also may simply not be appropriate for your data depending on properties of the data that can only be seen with an example. In the future, when asking for help with code, please show example data. And use the -dataex- command to do that. 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