Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <njcoxstata@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | st: listing only a few observations that satisfy a condition |
Date | Mon, 12 Nov 2012 00:21:31 +0000 |
Over at <http://stackoverflow.com/questions/13272572/stata-list-only-few-number-of-records-with-condition> "giordano" asked "I would like to list only few numbers of records with some conditions. Problem: if I use in 1/4 or _n <= 4 and the first 4 records do not satisfy the condition no records are listed. Here is an example: clear input x 1 2 3 4 5 6 end list if x > 4 & _n <= list in 1/3 if x > 4 Does anybody has an idea how can be solved this problem without in one line?" Interesting question. You want just to see examples, not a complete list. -codebook- has a little of that flavour, but what else? The problem is that "in 1/4" (e.g.) doesn't mean show me the first four examples. Dimitriy Masterov posted a short program. Here's another program, but examples first . sysuse auto (1978 Automobile Data) . eglist mpg (74 observations) +-----+ | mpg | |-----| 1. | 22 | 2. | 17 | 3. | 22 | 4. | 20 | 5. | 15 | +-----+ . eglist mpg if foreign (22 observations) +-----+ | mpg | |-----| 53. | 17 | 54. | 23 | 55. | 25 | 56. | 23 | 57. | 35 | +-----+ . eglist mpg if foreign, last (22 observations) +-----+ | mpg | |-----| 70. | 23 | 71. | 41 | 72. | 25 | 73. | 25 | 74. | 17 | +-----+ *! 1.0.0 NJC 9 Nov 2012 program eglist, sortpreserve byable(recall) version 8 syntax varlist [if] [in] [ , n(int 5) MISSing LAST * ] marksample touse, novarlist if _by() & "`missing'" == "" { markout `touse' `_byvars', strok } quietly { count if `touse' if r(N) == 0 error 2000 local N = r(N) replace `touse' = cond(`touse', sum(`touse'), .) } di as text "(`N' observations)" if "`last'" == "" { list `varlist' if inrange(`touse', 1, `n'), `options' } else list `varlist' if inrange(`touse', `N' - `n' + 1, `N') end I'm probably missing something here. Nick * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/