Statalist The Stata Listserver


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

RE: st: Map logical operator across varlist?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Map logical operator across varlist?
Date   Wed, 17 Jan 2007 20:59:36 -0000

Scheme is according to taste a variant, relative or
dialect of Lisp with "lots of irritating superfluous 
parentheses", as one story goes. Stata is in the lineage
of C and so on the whole its constructs are not similar
to those of Scheme. (Similar story for S.) 

In this particular case you want observations that 
have values that are not missing on all the variables 
in a list. And you also asked about -egen-. As you
are aware of -egen-, you could also have looked at 
the help to discover pertinent functions. The easiest 
is probably -egen, rowmiss()-. 

egen nmiss = rowmiss(med*) 

counts missings across a varlist so that the condition 
you want then becomes 

	if !nmiss 

as -nmiss- of 0 means present on all variables, -nmiss-
of 1 or more means missing on at least 1 variable and 
negation flips true to false and vice versa. 

If -egen- did not exist, you would not really need
to reinvent it. 

gen anymiss = 0 

quietly foreach v of var med* { 
	replace anymiss = anymiss | missing(`v') 
} 

gets you where you want to be (logically) so 
that 

	if !anymiss

is then an appropriate condition. The same kind 
of construct could be used with the operator &
for other problems. Similarly the 
guts of -egen, rowmiss()- are, applied to your 
variables, 

gen nmiss = 0 

quietly foreach v of var med* { 
	replace nmiss = nmiss + missing(`v')
}

David Kantor's module -trinary- is intriguing 
stuff, but it seems to me that your problems 
require only official Stata.  

Nick 
[email protected] 

David Kantor
 
> I don't think you can "map" an arbitrary operator.
> 
> But there are many ways to program your problem.
> 
> If you create a set of indicators as to whether the med variables are 
> nonmissing (and by the way, a more generally correct test is 
> ~mi() )...
>   gen byte nonmis_med1 = ~mi(med1)
>   gen byte nonmis_med2 = ~mi(med2)
>    etc., (and that can be made into a loop)
> 
> then if you have the trinary module, you can do
> 
> egen byte med_ok = rtvor(nonmis_*)
> 
> this will give you the row-or of the arguments.  (The fact that it is 
> 3-valued logic is irrelevant.)
> Then you can
> list med* if med_ok
> 
> 
> To get trinary,
> ssc inst trinary
 
Joel J. Adamson 

> >Does anyone know how to map a logical operation (e.g., | or &) across
> >a list so that I can save myself some typing?
> >
> >Let me give you an example (the one that prompted the search for an
> >answer):
> >
> >I want to list a set of variables, only if an observation  
> is not missing
> >for the variables in question.  We usually handle this by 
> constructing
> >a small logical expression:
> >
> >l med1 med2 if (med1~=.|med2~=.)
> >
> >However, if I have six, or a hundred "med" variables (med*), it would
> >be easier (less error-prone) to type:
> >
> >l med* if |(med*~=.)
> >
> >Where the "|" before the "(" maps the operation onto the 
> expanded list
> >of variables.
> >
> >I know that Scheme (and hence probably S) have a function 
> called "map"
> >that maps any operation onto a list, e.g.,
> >
> >(map or '(1 2 3 ... ))
> >
> >Does anyone know of a similar construct in Stata?  How about an egen
> >function that would accomplish the same goal?

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