Announcement

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

  • combining commands to keep and drop certain observations

    I would be grateful for advice on whether I need to execute the commands step by step (in what sequence?) or if I combine all the commands. I have 6000 observations in total but only need to segregate those in specific ages (currently they are continuous) which are 50 and above. Out of these observations, I need to drop those who are retired and leave with only 3 other categories (work full time, work part time and unemployed). Then I need to further subdivide them into 4 age categories 50-54, 55-59, 60-64, 65+. How should I do it? Thank you so much.

  • #2
    You can combine the keep and drop operations into a single command. It would look something like this:
    Code:
    keep if age >= 50 & work_status != whatever_the_value_corresponding_to_retired_is
    To subdivide into four age categories, you can do something like:
    Code:
    gen age_cat = 1 if age <= 54
    replace age_cat = 2 if age <= 59
    replace age_cat = 3 if age <= 64
    replace age_cat = 4 if age >= 65 & !missing(age)
    You might want to create a value label for the age_cat variable so it is easier to work with in the browser and in listings.

    If you need advice that is more tailored to your actual data, you need to show example data, using 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