Statalist


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

RE: st: list (if) (in)


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: list (if) (in)
Date   Thu, 10 Apr 2008 20:15:36 +0100

Sergiy's solution has two big advantages: 

1. Short. 
2. Sweet. 

And two big disadvantages: 

1. Destroys the original data. That's easy to fix: 

preserve 
keep if x == 5 
list in 1/10 
restore 

2. Observation numbers are no longer shown. That's also easy to fix: 

preserve 
gen _obs = _n 
keep if x == 5 
list in 1/10, noobs 
restore 

where naturally _obs, or whatever, must be a new variable name. 

Beyond that, there is an intriguing challenge. How far can you go in
programming an alternative -list- 
in which -if- keeps its usual meaning but -in- now is conditional on the
-if-? 

Here is one track followed: 

First, some results:

. sysuse auto

. listsome make mpg if foreign in 1/10

     +--------------------+
     | make           mpg |
     |--------------------|
 53. | Audi 5000       17 |
 54. | Audi Fox        23 |
 55. | BMW 320i        25 |
 56. | Datsun 200      23 |
 57. | Datsun 210      35 |
     |--------------------|
 58. | Datsun 510      24 |
 59. | Datsun 810      21 |
 60. | Fiat Strada     21 |
 61. | Honda Accord    25 |
 62. | Honda Civic     28 |
     +--------------------+

. listsome make mpg if foreign in 11/20

     +----------------------+
     | make             mpg |
     |----------------------|
 63. | Mazda GLC         30 |
 64. | Peugeot 604       14 |
 65. | Renault Le Car    26 |
 66. | Subaru            35 |
 67. | Toyota Celica     18 |
     |----------------------|
 68. | Toyota Corolla    31 |
 69. | Toyota Corona     18 |
 70. | VW Dasher         23 |
 71. | VW Diesel         41 |
 72. | VW Rabbit         25 |
     +----------------------+

Second, what is -listsome-? 

program listsome
	version 8 
	syntax [varlist(def=all)] [if/] [in/] [, * ] 

	quietly {
		tempvar OK 
		if `"`if'"' == "" local if 1 
		gen byte `OK' = `if'
		replace `OK' = cond(`OK', sum(`OK'), 0) 

		if "`in'" == "" { 
			local first 1
			local last = _N 
		} 
		else { 
			tokenize `in', parse("/") 
			args first slash last 
		} 		
	}
	
	list `varlist' if inrange(`OK', `first', `last'), `options' 
end 	

The idea is that -listsome- without one or both of -if- or -in- should
behave as -list- 
does. But with both -if- and -in-, the -in- is applied conditionally on
the -in-. 

Here -in- like -10/-1 or -10/l or -10/L doesn't usually work the way you
might want it. -syntax- 
grabs those and converts them into absolute observation numbers before
you can subvert it. 
Of course, you could put the -in- arguments in an option, but that has
no appeal to me. 

Nick
[email protected] 

Sergiy Radyakin

keep if x==5
list in 1/10

On 4/10/08, Malcolm Wardlaw <[email protected]> wrote:

> I'm sure this question is easy to answer, but searching for it is a
little
> hard given that there are a lot of "if"s and "in"s in the
documentation.
>
> I want to be able to list the first 10 observations that conform to a
given
> 'if' statement.
>
> I can't do
>
>        list if x=5 in 1/10
>
> or
>
>        list in 1/10 if x=5
>
> since that only returns the first 10 observations in the data -if-
they
> conform.
>
> I want the first 10 observations that -do- conform.
>
> Is there an easy way to do this?

*
*   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