Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: keep variables


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: keep variables
Date   Tue, 8 Jan 2008 15:11:58 -0000

I strongly agree with Svend's general stance here. This is not 
a stupid question, and it requires some Stata technique. 

I offer a condensation to one line: 

bysort id (analt): keep if episodi[1] == 1 & analt > 2001

Under -by-, with the sort order specified, the subscript [1] refers to
the first year observed. This device cuts out the creation of extra
variables. 

Nick 
[email protected] 

Svend Juul

Catia wrote:

I have a simple and stupid problem to selection some variables, 
but I don't know as to resolve this problem:
I have this dataset

id analt episodi
1 1999 1
1 2002 2
2 2002 1
2 2003 2
2 2003 3
3 2004 1
3 2005 2

I want to selection all the id that analt>2001 & the first 
episodi start with ==1, so I want to take id 2 and id 3

id analt episodi
2 2002 1
2 2002 2
3 2004 1
3 2005 2

---------------------------------------------------------

Your question is about keeping observations, not variables.
You can use the -egen min()- function to create a variable
that is constant for each id:

   sort id analt episodi
   by id: egen firstyear = min(analt)
   by id: egen firstepi = min(episodi)
   keep if firstyear>2001 & firstepi==1

The problem is not stupid, it is just a matter of experience.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index