Announcement

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

  • Keep if or Drop If

    Hello,

    I am trying to select a couple of cases in a dataset - and perform a set of operations/changes on those cases. In SPSS, I can use SELECT IF to temporarily restrict operations to cases fiting that criteria. After saving the operations, I can remove the restrictions to work on the full dataset.

    In STATA, I think I can temporarily use KEEP IF to temporarily restrict operations to data satisfying the IF condition. But once I am done with operations on those temporarily selected cases, how can I remove the restrictions and save changes to the full dataset?


    Thanks for any advance - cY

  • #2
    Although you could manipulate the dataset, this is usually not necessary in Stata. Most of Stata's commands for data manipulation (and estimation as well) support the if qualifier (see: help if). If you provide an example of what you are trying to do, we might be able to suggest code.

    Best
    Daniel

    Comment


    • #3
      I normally create a variable call sample1, sample2, sample3, etc that include the cases fitting the criteria (be sure to label those variables). The you can run your regressions where sample1==1, sample2==1, and so on. That makes it easy to run your regressions on a subset, but not delete any observations.

      NOTE: This probably only makes sense if you have lots of criteria to be included in the sample. Otherwise, it's just as easy (as Daniel mentions) to just use the if qualifier:

      Code:
      // Regression using the if qualifier
      reg y_var x1 x2 x3 if female==1 & age>=18 & age<30
      
      // Creating a sample1, sample2, sample3, etc
      gen sample1 = (female==1 & age>=18 & age<30)
      gen sample2 = (female==1 & age>=30 & age<45)
      
      reg y_var x1 x2 x3 if sample1==1
      reg y_var x1 x2 x3 if sample2==1
      Last edited by David Benson; 26 Oct 2019, 14:07.

      Comment

      Working...
      X