Announcement

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

  • Dropping a group of Observations

    Dear All,

    Kindly assist with a code for dropping observations in a group of variable, based on a condition. I am working with a panel data of 5 years, looking at household electricity consumption. I would like to analyse the data only for people with access to electricity. The electricity access variable is a dummy. If I say keep if electricity access==1, observations do get dropped but the panel becomes unbalanced. So I would like all the variables to only be for people with access to electricity.

    Thanking you in advance.

    Regards,

  • #2
    I find your explanation unclear. Do you mean that you want to keep a household only if it has electricity access in every year in the data set? If that's what you want:

    Code:
    by hhid (electricity_access): keep if electricity_access[1] == 1
    Note: Assumes the electricity _access variable is coded as yes = 1, no = 0. Yes = 1, no = missing value will not work with this code, and is generally a bad idea for coding dichotomies in Stata.

    If that is not what you want, or if this does not run properly with your data, when posting back:
    1. Use the -dataex- command to show example data from your data set. Make sure the example illustrates any errors or problems you are encountering with the code so troubleshooting will be possible. 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.
    2. Provide a clearer explanation of what you want to do. Actually, even better than explaining is showing. Give examples of several households, some of which you wish to keep and some of which you wish to drop. Show which is which.
    Finally, I'll just point out that nearly all Stata panel-data commands work well with unbalanced panel data. When you eliminate the unbalanced panels from your sample, regardless of how or why, you risk introducing selection bias into your data set. So unless you know of a specific problem that your planned analyses will encounter with unbalanced data, you are probably better off leaving things the way they are.

    Comment


    • #3
      Dear Clyde,

      That is what I meant, thanks a lot. I will definitely use dates next time.

      Regards,

      Comment

      Working...
      X